Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_H |
| 18 | #define _UI_INPUT_READER_H |
| 19 | |
| 20 | #include "EventHub.h" |
| 21 | #include "PointerControllerInterface.h" |
| 22 | #include "InputListener.h" |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 23 | #include "InputReaderBase.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 25 | #include <input/DisplayViewport.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 26 | #include <input/Input.h> |
| 27 | #include <input/VelocityControl.h> |
| 28 | #include <input/VelocityTracker.h> |
| 29 | #include <ui/DisplayInfo.h> |
| 30 | #include <utils/KeyedVector.h> |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 31 | #include <utils/Condition.h> |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 32 | #include <utils/Mutex.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | #include <utils/Timers.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #include <utils/BitSet.h> |
| 35 | |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 36 | #include <optional> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | #include <stddef.h> |
| 38 | #include <unistd.h> |
Siarhei Vishniakou | d634392 | 2018-07-06 23:33:37 +0100 | [diff] [blame] | 39 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 40 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 | namespace android { |
| 42 | |
| 43 | class InputDevice; |
| 44 | class InputMapper; |
| 45 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 47 | struct StylusState { |
| 48 | /* Time the stylus event was received. */ |
| 49 | nsecs_t when; |
| 50 | /* Pressure as reported by the stylus, normalized to the range [0, 1.0]. */ |
| 51 | float pressure; |
| 52 | /* The state of the stylus buttons as a bitfield (e.g. AMOTION_EVENT_BUTTON_SECONDARY). */ |
| 53 | uint32_t buttons; |
| 54 | /* Which tool type the stylus is currently using (e.g. AMOTION_EVENT_TOOL_TYPE_ERASER). */ |
| 55 | int32_t toolType; |
| 56 | |
| 57 | void copyFrom(const StylusState& other) { |
| 58 | when = other.when; |
| 59 | pressure = other.pressure; |
| 60 | buttons = other.buttons; |
| 61 | toolType = other.toolType; |
| 62 | } |
| 63 | |
| 64 | void clear() { |
| 65 | when = LLONG_MAX; |
| 66 | pressure = 0.f; |
| 67 | buttons = 0; |
| 68 | toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 69 | } |
| 70 | }; |
| 71 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 72 | |
| 73 | /* Internal interface used by individual input devices to access global input device state |
| 74 | * and parameters maintained by the input reader. |
| 75 | */ |
| 76 | class InputReaderContext { |
| 77 | public: |
| 78 | InputReaderContext() { } |
| 79 | virtual ~InputReaderContext() { } |
| 80 | |
| 81 | virtual void updateGlobalMetaState() = 0; |
| 82 | virtual int32_t getGlobalMetaState() = 0; |
| 83 | |
| 84 | virtual void disableVirtualKeysUntil(nsecs_t time) = 0; |
| 85 | virtual bool shouldDropVirtualKey(nsecs_t now, |
| 86 | InputDevice* device, int32_t keyCode, int32_t scanCode) = 0; |
| 87 | |
| 88 | virtual void fadePointer() = 0; |
| 89 | |
| 90 | virtual void requestTimeoutAtTime(nsecs_t when) = 0; |
| 91 | virtual int32_t bumpGeneration() = 0; |
| 92 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 93 | virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) = 0; |
Michael Wright | b85401d | 2015-04-17 18:35:15 +0100 | [diff] [blame] | 94 | virtual void dispatchExternalStylusState(const StylusState& outState) = 0; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 95 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | virtual InputReaderPolicyInterface* getPolicy() = 0; |
| 97 | virtual InputListenerInterface* getListener() = 0; |
| 98 | virtual EventHubInterface* getEventHub() = 0; |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 99 | |
| 100 | virtual uint32_t getNextSequenceNum() = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | |
| 104 | /* The input reader reads raw event data from the event hub and processes it into input events |
| 105 | * that it sends to the input listener. Some functions of the input reader, such as early |
| 106 | * event filtering in low power states, are controlled by a separate policy object. |
| 107 | * |
| 108 | * The InputReader owns a collection of InputMappers. Most of the work it does happens |
| 109 | * on the input reader thread but the InputReader can receive queries from other system |
| 110 | * components running on arbitrary threads. To keep things manageable, the InputReader |
| 111 | * uses a single Mutex to guard its state. The Mutex may be held while calling into the |
| 112 | * EventHub or the InputReaderPolicy but it is never held while calling into the |
| 113 | * InputListener. |
| 114 | */ |
| 115 | class InputReader : public InputReaderInterface { |
| 116 | public: |
| 117 | InputReader(const sp<EventHubInterface>& eventHub, |
| 118 | const sp<InputReaderPolicyInterface>& policy, |
| 119 | const sp<InputListenerInterface>& listener); |
| 120 | virtual ~InputReader(); |
| 121 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 122 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 123 | virtual void monitor(); |
| 124 | |
| 125 | virtual void loopOnce(); |
| 126 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 127 | virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 129 | virtual bool isInputDeviceEnabled(int32_t deviceId); |
| 130 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 132 | int32_t scanCode); |
| 133 | virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 134 | int32_t keyCode); |
| 135 | virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, |
| 136 | int32_t sw); |
| 137 | |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 138 | virtual void toggleCapsLockState(int32_t deviceId); |
| 139 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 141 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags); |
| 142 | |
| 143 | virtual void requestRefreshConfiguration(uint32_t changes); |
| 144 | |
| 145 | virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, |
| 146 | ssize_t repeat, int32_t token); |
| 147 | virtual void cancelVibrate(int32_t deviceId, int32_t token); |
| 148 | |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 149 | virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | protected: |
| 151 | // These members are protected so they can be instrumented by test cases. |
| 152 | virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber, |
| 153 | const InputDeviceIdentifier& identifier, uint32_t classes); |
| 154 | |
| 155 | class ContextImpl : public InputReaderContext { |
| 156 | InputReader* mReader; |
| 157 | |
| 158 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 159 | explicit ContextImpl(InputReader* reader); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | |
| 161 | virtual void updateGlobalMetaState(); |
| 162 | virtual int32_t getGlobalMetaState(); |
| 163 | virtual void disableVirtualKeysUntil(nsecs_t time); |
| 164 | virtual bool shouldDropVirtualKey(nsecs_t now, |
| 165 | InputDevice* device, int32_t keyCode, int32_t scanCode); |
| 166 | virtual void fadePointer(); |
| 167 | virtual void requestTimeoutAtTime(nsecs_t when); |
| 168 | virtual int32_t bumpGeneration(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 169 | virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 170 | virtual void dispatchExternalStylusState(const StylusState& outState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | virtual InputReaderPolicyInterface* getPolicy(); |
| 172 | virtual InputListenerInterface* getListener(); |
| 173 | virtual EventHubInterface* getEventHub(); |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 174 | virtual uint32_t getNextSequenceNum(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | } mContext; |
| 176 | |
| 177 | friend class ContextImpl; |
| 178 | |
| 179 | private: |
| 180 | Mutex mLock; |
| 181 | |
| 182 | Condition mReaderIsAliveCondition; |
| 183 | |
| 184 | sp<EventHubInterface> mEventHub; |
| 185 | sp<InputReaderPolicyInterface> mPolicy; |
| 186 | sp<QueuedInputListener> mQueuedListener; |
| 187 | |
| 188 | InputReaderConfiguration mConfig; |
| 189 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 190 | // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers |
| 191 | uint32_t mNextSequenceNum; |
| 192 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | // The event queue. |
| 194 | static const int EVENT_BUFFER_SIZE = 256; |
| 195 | RawEvent mEventBuffer[EVENT_BUFFER_SIZE]; |
| 196 | |
| 197 | KeyedVector<int32_t, InputDevice*> mDevices; |
| 198 | |
| 199 | // low-level input event decoding and device management |
| 200 | void processEventsLocked(const RawEvent* rawEvents, size_t count); |
| 201 | |
| 202 | void addDeviceLocked(nsecs_t when, int32_t deviceId); |
| 203 | void removeDeviceLocked(nsecs_t when, int32_t deviceId); |
| 204 | void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count); |
| 205 | void timeoutExpiredLocked(nsecs_t when); |
| 206 | |
| 207 | void handleConfigurationChangedLocked(nsecs_t when); |
| 208 | |
| 209 | int32_t mGlobalMetaState; |
| 210 | void updateGlobalMetaStateLocked(); |
| 211 | int32_t getGlobalMetaStateLocked(); |
| 212 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 213 | void notifyExternalStylusPresenceChanged(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 214 | void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 215 | void dispatchExternalStylusState(const StylusState& state); |
| 216 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 217 | void fadePointerLocked(); |
| 218 | |
| 219 | int32_t mGeneration; |
| 220 | int32_t bumpGenerationLocked(); |
| 221 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 222 | void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | |
| 224 | nsecs_t mDisableVirtualKeysTimeout; |
| 225 | void disableVirtualKeysUntilLocked(nsecs_t time); |
| 226 | bool shouldDropVirtualKeyLocked(nsecs_t now, |
| 227 | InputDevice* device, int32_t keyCode, int32_t scanCode); |
| 228 | |
| 229 | nsecs_t mNextTimeout; |
| 230 | void requestTimeoutAtTimeLocked(nsecs_t when); |
| 231 | |
| 232 | uint32_t mConfigurationChangesToRefresh; |
| 233 | void refreshConfigurationLocked(uint32_t changes); |
| 234 | |
| 235 | // state queries |
| 236 | typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 237 | int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 238 | GetStateFunc getStateFunc); |
| 239 | bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 240 | const int32_t* keyCodes, uint8_t* outFlags); |
| 241 | }; |
| 242 | |
| 243 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | /* Represents the state of a single input device. */ |
| 245 | class InputDevice { |
| 246 | public: |
| 247 | InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t |
| 248 | controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes); |
| 249 | ~InputDevice(); |
| 250 | |
| 251 | inline InputReaderContext* getContext() { return mContext; } |
| 252 | inline int32_t getId() const { return mId; } |
| 253 | inline int32_t getControllerNumber() const { return mControllerNumber; } |
| 254 | inline int32_t getGeneration() const { return mGeneration; } |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 255 | inline const std::string getName() const { return mIdentifier.name; } |
| 256 | inline const std::string getDescriptor() { return mIdentifier.descriptor; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 257 | inline uint32_t getClasses() const { return mClasses; } |
| 258 | inline uint32_t getSources() const { return mSources; } |
| 259 | |
| 260 | inline bool isExternal() { return mIsExternal; } |
| 261 | inline void setExternal(bool external) { mIsExternal = external; } |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 262 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 263 | return mAssociatedDisplayPort; |
| 264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 265 | |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 266 | inline void setMic(bool hasMic) { mHasMic = hasMic; } |
| 267 | inline bool hasMic() const { return mHasMic; } |
| 268 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 269 | inline bool isIgnored() { return mMappers.empty(); } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 270 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 271 | bool isEnabled(); |
| 272 | void setEnabled(bool enabled, nsecs_t when); |
| 273 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 274 | void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 275 | void addMapper(InputMapper* mapper); |
| 276 | void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 277 | void reset(nsecs_t when); |
| 278 | void process(const RawEvent* rawEvents, size_t count); |
| 279 | void timeoutExpired(nsecs_t when); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 280 | void updateExternalStylusState(const StylusState& state); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 281 | |
| 282 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 283 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 284 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 285 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 286 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 287 | const int32_t* keyCodes, uint8_t* outFlags); |
| 288 | void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token); |
| 289 | void cancelVibrate(int32_t token); |
Jeff Brown | c9aa628 | 2015-02-11 19:03:28 -0800 | [diff] [blame] | 290 | void cancelTouch(nsecs_t when); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 291 | |
| 292 | int32_t getMetaState(); |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 293 | void updateMetaState(int32_t keyCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 294 | |
| 295 | void fadePointer(); |
| 296 | |
| 297 | void bumpGeneration(); |
| 298 | |
| 299 | void notifyReset(nsecs_t when); |
| 300 | |
| 301 | inline const PropertyMap& getConfiguration() { return mConfiguration; } |
| 302 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 303 | |
| 304 | bool hasKey(int32_t code) { |
| 305 | return getEventHub()->hasScanCode(mId, code); |
| 306 | } |
| 307 | |
| 308 | bool hasAbsoluteAxis(int32_t code) { |
| 309 | RawAbsoluteAxisInfo info; |
| 310 | getEventHub()->getAbsoluteAxisInfo(mId, code, &info); |
| 311 | return info.valid; |
| 312 | } |
| 313 | |
| 314 | bool isKeyPressed(int32_t code) { |
| 315 | return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN; |
| 316 | } |
| 317 | |
| 318 | int32_t getAbsoluteAxisValue(int32_t code) { |
| 319 | int32_t value; |
| 320 | getEventHub()->getAbsoluteAxisValue(mId, code, &value); |
| 321 | return value; |
| 322 | } |
| 323 | |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 324 | std::optional<int32_t> getAssociatedDisplay(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 325 | private: |
| 326 | InputReaderContext* mContext; |
| 327 | int32_t mId; |
| 328 | int32_t mGeneration; |
| 329 | int32_t mControllerNumber; |
| 330 | InputDeviceIdentifier mIdentifier; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 331 | std::string mAlias; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 332 | uint32_t mClasses; |
| 333 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 334 | std::vector<InputMapper*> mMappers; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 335 | |
| 336 | uint32_t mSources; |
| 337 | bool mIsExternal; |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 338 | std::optional<uint8_t> mAssociatedDisplayPort; |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 339 | bool mHasMic; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 340 | bool mDropUntilNextSync; |
| 341 | |
| 342 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 343 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
| 344 | |
| 345 | PropertyMap mConfiguration; |
| 346 | }; |
| 347 | |
| 348 | |
| 349 | /* Keeps track of the state of mouse or touch pad buttons. */ |
| 350 | class CursorButtonAccumulator { |
| 351 | public: |
| 352 | CursorButtonAccumulator(); |
| 353 | void reset(InputDevice* device); |
| 354 | |
| 355 | void process(const RawEvent* rawEvent); |
| 356 | |
| 357 | uint32_t getButtonState() const; |
| 358 | |
| 359 | private: |
| 360 | bool mBtnLeft; |
| 361 | bool mBtnRight; |
| 362 | bool mBtnMiddle; |
| 363 | bool mBtnBack; |
| 364 | bool mBtnSide; |
| 365 | bool mBtnForward; |
| 366 | bool mBtnExtra; |
| 367 | bool mBtnTask; |
| 368 | |
| 369 | void clearButtons(); |
| 370 | }; |
| 371 | |
| 372 | |
| 373 | /* Keeps track of cursor movements. */ |
| 374 | |
| 375 | class CursorMotionAccumulator { |
| 376 | public: |
| 377 | CursorMotionAccumulator(); |
| 378 | void reset(InputDevice* device); |
| 379 | |
| 380 | void process(const RawEvent* rawEvent); |
| 381 | void finishSync(); |
| 382 | |
| 383 | inline int32_t getRelativeX() const { return mRelX; } |
| 384 | inline int32_t getRelativeY() const { return mRelY; } |
| 385 | |
| 386 | private: |
| 387 | int32_t mRelX; |
| 388 | int32_t mRelY; |
| 389 | |
| 390 | void clearRelativeAxes(); |
| 391 | }; |
| 392 | |
| 393 | |
| 394 | /* Keeps track of cursor scrolling motions. */ |
| 395 | |
| 396 | class CursorScrollAccumulator { |
| 397 | public: |
| 398 | CursorScrollAccumulator(); |
| 399 | void configure(InputDevice* device); |
| 400 | void reset(InputDevice* device); |
| 401 | |
| 402 | void process(const RawEvent* rawEvent); |
| 403 | void finishSync(); |
| 404 | |
| 405 | inline bool haveRelativeVWheel() const { return mHaveRelWheel; } |
| 406 | inline bool haveRelativeHWheel() const { return mHaveRelHWheel; } |
| 407 | |
| 408 | inline int32_t getRelativeX() const { return mRelX; } |
| 409 | inline int32_t getRelativeY() const { return mRelY; } |
| 410 | inline int32_t getRelativeVWheel() const { return mRelWheel; } |
| 411 | inline int32_t getRelativeHWheel() const { return mRelHWheel; } |
| 412 | |
| 413 | private: |
| 414 | bool mHaveRelWheel; |
| 415 | bool mHaveRelHWheel; |
| 416 | |
| 417 | int32_t mRelX; |
| 418 | int32_t mRelY; |
| 419 | int32_t mRelWheel; |
| 420 | int32_t mRelHWheel; |
| 421 | |
| 422 | void clearRelativeAxes(); |
| 423 | }; |
| 424 | |
| 425 | |
| 426 | /* Keeps track of the state of touch, stylus and tool buttons. */ |
| 427 | class TouchButtonAccumulator { |
| 428 | public: |
| 429 | TouchButtonAccumulator(); |
| 430 | void configure(InputDevice* device); |
| 431 | void reset(InputDevice* device); |
| 432 | |
| 433 | void process(const RawEvent* rawEvent); |
| 434 | |
| 435 | uint32_t getButtonState() const; |
| 436 | int32_t getToolType() const; |
| 437 | bool isToolActive() const; |
| 438 | bool isHovering() const; |
| 439 | bool hasStylus() const; |
| 440 | |
| 441 | private: |
| 442 | bool mHaveBtnTouch; |
| 443 | bool mHaveStylus; |
| 444 | |
| 445 | bool mBtnTouch; |
| 446 | bool mBtnStylus; |
| 447 | bool mBtnStylus2; |
| 448 | bool mBtnToolFinger; |
| 449 | bool mBtnToolPen; |
| 450 | bool mBtnToolRubber; |
| 451 | bool mBtnToolBrush; |
| 452 | bool mBtnToolPencil; |
| 453 | bool mBtnToolAirbrush; |
| 454 | bool mBtnToolMouse; |
| 455 | bool mBtnToolLens; |
| 456 | bool mBtnToolDoubleTap; |
| 457 | bool mBtnToolTripleTap; |
| 458 | bool mBtnToolQuadTap; |
| 459 | |
| 460 | void clearButtons(); |
| 461 | }; |
| 462 | |
| 463 | |
| 464 | /* Raw axis information from the driver. */ |
| 465 | struct RawPointerAxes { |
| 466 | RawAbsoluteAxisInfo x; |
| 467 | RawAbsoluteAxisInfo y; |
| 468 | RawAbsoluteAxisInfo pressure; |
| 469 | RawAbsoluteAxisInfo touchMajor; |
| 470 | RawAbsoluteAxisInfo touchMinor; |
| 471 | RawAbsoluteAxisInfo toolMajor; |
| 472 | RawAbsoluteAxisInfo toolMinor; |
| 473 | RawAbsoluteAxisInfo orientation; |
| 474 | RawAbsoluteAxisInfo distance; |
| 475 | RawAbsoluteAxisInfo tiltX; |
| 476 | RawAbsoluteAxisInfo tiltY; |
| 477 | RawAbsoluteAxisInfo trackingId; |
| 478 | RawAbsoluteAxisInfo slot; |
| 479 | |
| 480 | RawPointerAxes(); |
Siarhei Vishniakou | 26e34d9 | 2018-11-12 13:51:26 -0800 | [diff] [blame] | 481 | inline int32_t getRawWidth() const { return x.maxValue - x.minValue + 1; } |
| 482 | inline int32_t getRawHeight() const { return y.maxValue - y.minValue + 1; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 483 | void clear(); |
| 484 | }; |
| 485 | |
| 486 | |
| 487 | /* Raw data for a collection of pointers including a pointer id mapping table. */ |
| 488 | struct RawPointerData { |
| 489 | struct Pointer { |
| 490 | uint32_t id; |
| 491 | int32_t x; |
| 492 | int32_t y; |
| 493 | int32_t pressure; |
| 494 | int32_t touchMajor; |
| 495 | int32_t touchMinor; |
| 496 | int32_t toolMajor; |
| 497 | int32_t toolMinor; |
| 498 | int32_t orientation; |
| 499 | int32_t distance; |
| 500 | int32_t tiltX; |
| 501 | int32_t tiltY; |
| 502 | int32_t toolType; // a fully decoded AMOTION_EVENT_TOOL_TYPE constant |
| 503 | bool isHovering; |
| 504 | }; |
| 505 | |
| 506 | uint32_t pointerCount; |
| 507 | Pointer pointers[MAX_POINTERS]; |
| 508 | BitSet32 hoveringIdBits, touchingIdBits; |
| 509 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
| 510 | |
| 511 | RawPointerData(); |
| 512 | void clear(); |
| 513 | void copyFrom(const RawPointerData& other); |
| 514 | void getCentroidOfTouchingPointers(float* outX, float* outY) const; |
| 515 | |
| 516 | inline void markIdBit(uint32_t id, bool isHovering) { |
| 517 | if (isHovering) { |
| 518 | hoveringIdBits.markBit(id); |
| 519 | } else { |
| 520 | touchingIdBits.markBit(id); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | inline void clearIdBits() { |
| 525 | hoveringIdBits.clear(); |
| 526 | touchingIdBits.clear(); |
| 527 | } |
| 528 | |
| 529 | inline const Pointer& pointerForId(uint32_t id) const { |
| 530 | return pointers[idToIndex[id]]; |
| 531 | } |
| 532 | |
| 533 | inline bool isHovering(uint32_t pointerIndex) { |
| 534 | return pointers[pointerIndex].isHovering; |
| 535 | } |
| 536 | }; |
| 537 | |
| 538 | |
| 539 | /* Cooked data for a collection of pointers including a pointer id mapping table. */ |
| 540 | struct CookedPointerData { |
| 541 | uint32_t pointerCount; |
| 542 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 543 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 544 | BitSet32 hoveringIdBits, touchingIdBits; |
| 545 | uint32_t idToIndex[MAX_POINTER_ID + 1]; |
| 546 | |
| 547 | CookedPointerData(); |
| 548 | void clear(); |
| 549 | void copyFrom(const CookedPointerData& other); |
| 550 | |
| 551 | inline const PointerCoords& pointerCoordsForId(uint32_t id) const { |
| 552 | return pointerCoords[idToIndex[id]]; |
| 553 | } |
| 554 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 555 | inline PointerCoords& editPointerCoordsWithId(uint32_t id) { |
| 556 | return pointerCoords[idToIndex[id]]; |
| 557 | } |
| 558 | |
| 559 | inline PointerProperties& editPointerPropertiesWithId(uint32_t id) { |
| 560 | return pointerProperties[idToIndex[id]]; |
| 561 | } |
| 562 | |
Michael Wright | 53dca3a | 2015-04-23 17:39:53 +0100 | [diff] [blame] | 563 | inline bool isHovering(uint32_t pointerIndex) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | return hoveringIdBits.hasBit(pointerProperties[pointerIndex].id); |
| 565 | } |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 566 | |
Michael Wright | 53dca3a | 2015-04-23 17:39:53 +0100 | [diff] [blame] | 567 | inline bool isTouching(uint32_t pointerIndex) const { |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 568 | return touchingIdBits.hasBit(pointerProperties[pointerIndex].id); |
| 569 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | }; |
| 571 | |
Siarhei Vishniakou | 9ffab0c | 2018-11-08 19:54:22 -0800 | [diff] [blame] | 572 | /** |
| 573 | * Basic statistics information. |
| 574 | * Keep track of min, max, average, and standard deviation of the received samples. |
| 575 | * Used to report latency information about input events. |
| 576 | */ |
| 577 | struct LatencyStatistics { |
| 578 | float min; |
| 579 | float max; |
| 580 | // Sum of all samples |
| 581 | float sum; |
| 582 | // Sum of squares of all samples |
| 583 | float sum2; |
| 584 | // The number of samples |
| 585 | size_t count; |
| 586 | // The last time statistics were reported. |
| 587 | nsecs_t lastReportTime; |
| 588 | |
| 589 | LatencyStatistics() { |
| 590 | reset(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 591 | } |
| 592 | |
| 593 | inline void addValue(float x) { |
| 594 | if (x < min) { |
| 595 | min = x; |
| 596 | } |
| 597 | if (x > max) { |
| 598 | max = x; |
| 599 | } |
| 600 | sum += x; |
| 601 | sum2 += x * x; |
| 602 | count++; |
| 603 | } |
| 604 | |
| 605 | // Get the average value. Should not be called if no samples have been added. |
| 606 | inline float mean() { |
| 607 | if (count == 0) { |
| 608 | return 0; |
| 609 | } |
| 610 | return sum / count; |
| 611 | } |
| 612 | |
| 613 | // Get the standard deviation. Should not be called if no samples have been added. |
| 614 | inline float stdev() { |
| 615 | if (count == 0) { |
| 616 | return 0; |
| 617 | } |
| 618 | float average = mean(); |
| 619 | return sqrt(sum2 / count - average * average); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Reset internal state. The variable 'when' is the time when the data collection started. |
| 624 | * Call this to start a new data collection window. |
| 625 | */ |
| 626 | inline void reset(nsecs_t when) { |
| 627 | max = 0; |
| 628 | min = std::numeric_limits<float>::max(); |
| 629 | sum = 0; |
| 630 | sum2 = 0; |
| 631 | count = 0; |
| 632 | lastReportTime = when; |
| 633 | } |
| 634 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 635 | |
| 636 | /* Keeps track of the state of single-touch protocol. */ |
| 637 | class SingleTouchMotionAccumulator { |
| 638 | public: |
| 639 | SingleTouchMotionAccumulator(); |
| 640 | |
| 641 | void process(const RawEvent* rawEvent); |
| 642 | void reset(InputDevice* device); |
| 643 | |
| 644 | inline int32_t getAbsoluteX() const { return mAbsX; } |
| 645 | inline int32_t getAbsoluteY() const { return mAbsY; } |
| 646 | inline int32_t getAbsolutePressure() const { return mAbsPressure; } |
| 647 | inline int32_t getAbsoluteToolWidth() const { return mAbsToolWidth; } |
| 648 | inline int32_t getAbsoluteDistance() const { return mAbsDistance; } |
| 649 | inline int32_t getAbsoluteTiltX() const { return mAbsTiltX; } |
| 650 | inline int32_t getAbsoluteTiltY() const { return mAbsTiltY; } |
| 651 | |
| 652 | private: |
| 653 | int32_t mAbsX; |
| 654 | int32_t mAbsY; |
| 655 | int32_t mAbsPressure; |
| 656 | int32_t mAbsToolWidth; |
| 657 | int32_t mAbsDistance; |
| 658 | int32_t mAbsTiltX; |
| 659 | int32_t mAbsTiltY; |
| 660 | |
| 661 | void clearAbsoluteAxes(); |
| 662 | }; |
| 663 | |
| 664 | |
| 665 | /* Keeps track of the state of multi-touch protocol. */ |
| 666 | class MultiTouchMotionAccumulator { |
| 667 | public: |
| 668 | class Slot { |
| 669 | public: |
| 670 | inline bool isInUse() const { return mInUse; } |
| 671 | inline int32_t getX() const { return mAbsMTPositionX; } |
| 672 | inline int32_t getY() const { return mAbsMTPositionY; } |
| 673 | inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; } |
| 674 | inline int32_t getTouchMinor() const { |
| 675 | return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; } |
| 676 | inline int32_t getToolMajor() const { return mAbsMTWidthMajor; } |
| 677 | inline int32_t getToolMinor() const { |
| 678 | return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; } |
| 679 | inline int32_t getOrientation() const { return mAbsMTOrientation; } |
| 680 | inline int32_t getTrackingId() const { return mAbsMTTrackingId; } |
| 681 | inline int32_t getPressure() const { return mAbsMTPressure; } |
| 682 | inline int32_t getDistance() const { return mAbsMTDistance; } |
| 683 | inline int32_t getToolType() const; |
| 684 | |
| 685 | private: |
| 686 | friend class MultiTouchMotionAccumulator; |
| 687 | |
| 688 | bool mInUse; |
| 689 | bool mHaveAbsMTTouchMinor; |
| 690 | bool mHaveAbsMTWidthMinor; |
| 691 | bool mHaveAbsMTToolType; |
| 692 | |
| 693 | int32_t mAbsMTPositionX; |
| 694 | int32_t mAbsMTPositionY; |
| 695 | int32_t mAbsMTTouchMajor; |
| 696 | int32_t mAbsMTTouchMinor; |
| 697 | int32_t mAbsMTWidthMajor; |
| 698 | int32_t mAbsMTWidthMinor; |
| 699 | int32_t mAbsMTOrientation; |
| 700 | int32_t mAbsMTTrackingId; |
| 701 | int32_t mAbsMTPressure; |
| 702 | int32_t mAbsMTDistance; |
| 703 | int32_t mAbsMTToolType; |
| 704 | |
| 705 | Slot(); |
| 706 | void clear(); |
| 707 | }; |
| 708 | |
| 709 | MultiTouchMotionAccumulator(); |
| 710 | ~MultiTouchMotionAccumulator(); |
| 711 | |
| 712 | void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol); |
| 713 | void reset(InputDevice* device); |
| 714 | void process(const RawEvent* rawEvent); |
| 715 | void finishSync(); |
| 716 | bool hasStylus() const; |
| 717 | |
| 718 | inline size_t getSlotCount() const { return mSlotCount; } |
| 719 | inline const Slot* getSlot(size_t index) const { return &mSlots[index]; } |
| 720 | |
| 721 | private: |
| 722 | int32_t mCurrentSlot; |
| 723 | Slot* mSlots; |
| 724 | size_t mSlotCount; |
| 725 | bool mUsingSlotsProtocol; |
| 726 | bool mHaveStylus; |
| 727 | |
| 728 | void clearSlots(int32_t initialSlot); |
| 729 | }; |
| 730 | |
| 731 | |
| 732 | /* An input mapper transforms raw input events into cooked event data. |
| 733 | * A single input device can have multiple associated input mappers in order to interpret |
| 734 | * different classes of events. |
| 735 | * |
| 736 | * InputMapper lifecycle: |
| 737 | * - create |
| 738 | * - configure with 0 changes |
| 739 | * - reset |
| 740 | * - process, process, process (may occasionally reconfigure with non-zero changes or reset) |
| 741 | * - reset |
| 742 | * - destroy |
| 743 | */ |
| 744 | class InputMapper { |
| 745 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 746 | explicit InputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | virtual ~InputMapper(); |
| 748 | |
| 749 | inline InputDevice* getDevice() { return mDevice; } |
| 750 | inline int32_t getDeviceId() { return mDevice->getId(); } |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 751 | inline const std::string getDeviceName() { return mDevice->getName(); } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | inline InputReaderContext* getContext() { return mContext; } |
| 753 | inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); } |
| 754 | inline InputListenerInterface* getListener() { return mContext->getListener(); } |
| 755 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 756 | |
| 757 | virtual uint32_t getSources() = 0; |
| 758 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 759 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 760 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 761 | virtual void reset(nsecs_t when); |
| 762 | virtual void process(const RawEvent* rawEvent) = 0; |
| 763 | virtual void timeoutExpired(nsecs_t when); |
| 764 | |
| 765 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 766 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 767 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 768 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 769 | const int32_t* keyCodes, uint8_t* outFlags); |
| 770 | virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 771 | int32_t token); |
| 772 | virtual void cancelVibrate(int32_t token); |
Jeff Brown | c9aa628 | 2015-02-11 19:03:28 -0800 | [diff] [blame] | 773 | virtual void cancelTouch(nsecs_t when); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | |
| 775 | virtual int32_t getMetaState(); |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 776 | virtual void updateMetaState(int32_t keyCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 778 | virtual void updateExternalStylusState(const StylusState& state); |
| 779 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | virtual void fadePointer(); |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 781 | virtual std::optional<int32_t> getAssociatedDisplay() { |
| 782 | return std::nullopt; |
| 783 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 784 | protected: |
| 785 | InputDevice* mDevice; |
| 786 | InputReaderContext* mContext; |
| 787 | |
| 788 | status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo); |
| 789 | void bumpGeneration(); |
| 790 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 791 | static void dumpRawAbsoluteAxisInfo(std::string& dump, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | const RawAbsoluteAxisInfo& axis, const char* name); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 793 | static void dumpStylusState(std::string& dump, const StylusState& state); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | |
| 797 | class SwitchInputMapper : public InputMapper { |
| 798 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 799 | explicit SwitchInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | virtual ~SwitchInputMapper(); |
| 801 | |
| 802 | virtual uint32_t getSources(); |
| 803 | virtual void process(const RawEvent* rawEvent); |
| 804 | |
| 805 | virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 806 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | |
| 808 | private: |
Michael Wright | bcbf97e | 2014-08-29 14:31:32 -0700 | [diff] [blame] | 809 | uint32_t mSwitchValues; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 | uint32_t mUpdatedSwitchMask; |
| 811 | |
| 812 | void processSwitch(int32_t switchCode, int32_t switchValue); |
| 813 | void sync(nsecs_t when); |
| 814 | }; |
| 815 | |
| 816 | |
| 817 | class VibratorInputMapper : public InputMapper { |
| 818 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 819 | explicit VibratorInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 820 | virtual ~VibratorInputMapper(); |
| 821 | |
| 822 | virtual uint32_t getSources(); |
| 823 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
| 824 | virtual void process(const RawEvent* rawEvent); |
| 825 | |
| 826 | virtual void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 827 | int32_t token); |
| 828 | virtual void cancelVibrate(int32_t token); |
| 829 | virtual void timeoutExpired(nsecs_t when); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 830 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 831 | |
| 832 | private: |
| 833 | bool mVibrating; |
| 834 | nsecs_t mPattern[MAX_VIBRATE_PATTERN_SIZE]; |
| 835 | size_t mPatternSize; |
| 836 | ssize_t mRepeat; |
| 837 | int32_t mToken; |
| 838 | ssize_t mIndex; |
| 839 | nsecs_t mNextStepTime; |
| 840 | |
| 841 | void nextStep(); |
| 842 | void stopVibrating(); |
| 843 | }; |
| 844 | |
| 845 | |
| 846 | class KeyboardInputMapper : public InputMapper { |
| 847 | public: |
| 848 | KeyboardInputMapper(InputDevice* device, uint32_t source, int32_t keyboardType); |
| 849 | virtual ~KeyboardInputMapper(); |
| 850 | |
| 851 | virtual uint32_t getSources(); |
| 852 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 853 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 854 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 855 | virtual void reset(nsecs_t when); |
| 856 | virtual void process(const RawEvent* rawEvent); |
| 857 | |
| 858 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 859 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 860 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 861 | const int32_t* keyCodes, uint8_t* outFlags); |
| 862 | |
| 863 | virtual int32_t getMetaState(); |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 864 | virtual void updateMetaState(int32_t keyCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 | |
| 866 | private: |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 867 | // The current viewport. |
| 868 | std::optional<DisplayViewport> mViewport; |
| 869 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | struct KeyDown { |
| 871 | int32_t keyCode; |
| 872 | int32_t scanCode; |
| 873 | }; |
| 874 | |
| 875 | uint32_t mSource; |
| 876 | int32_t mKeyboardType; |
| 877 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 878 | std::vector<KeyDown> mKeyDowns; // keys that are down |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 879 | int32_t mMetaState; |
| 880 | nsecs_t mDownTime; // time of most recent key down |
| 881 | |
| 882 | int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none |
| 883 | |
| 884 | struct LedState { |
| 885 | bool avail; // led is available |
| 886 | bool on; // we think the led is currently on |
| 887 | }; |
| 888 | LedState mCapsLockLedState; |
| 889 | LedState mNumLockLedState; |
| 890 | LedState mScrollLockLedState; |
| 891 | |
| 892 | // Immutable configuration parameters. |
| 893 | struct Parameters { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 894 | bool orientationAware; |
Michael Wright | dcfcf5d | 2014-03-17 12:58:21 -0700 | [diff] [blame] | 895 | bool handlesKeyRepeat; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 896 | } mParameters; |
| 897 | |
| 898 | void configureParameters(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 899 | void dumpParameters(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 901 | int32_t getOrientation(); |
| 902 | int32_t getDisplayId(); |
| 903 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | bool isKeyboardOrGamepadKey(int32_t scanCode); |
Michael Wright | 58ba988 | 2017-07-26 16:19:11 +0100 | [diff] [blame] | 905 | bool isMediaKey(int32_t keyCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 907 | void processKey(nsecs_t when, bool down, int32_t scanCode, int32_t usageCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 909 | bool updateMetaStateIfNeeded(int32_t keyCode, bool down); |
| 910 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | ssize_t findKeyDown(int32_t scanCode); |
| 912 | |
| 913 | void resetLedState(); |
| 914 | void initializeLedState(LedState& ledState, int32_t led); |
| 915 | void updateLedState(bool reset); |
| 916 | void updateLedStateForModifier(LedState& ledState, int32_t led, |
| 917 | int32_t modifier, bool reset); |
| 918 | }; |
| 919 | |
| 920 | |
| 921 | class CursorInputMapper : public InputMapper { |
| 922 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 923 | explicit CursorInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 924 | virtual ~CursorInputMapper(); |
| 925 | |
| 926 | virtual uint32_t getSources(); |
| 927 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 928 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 929 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 930 | virtual void reset(nsecs_t when); |
| 931 | virtual void process(const RawEvent* rawEvent); |
| 932 | |
| 933 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 934 | |
| 935 | virtual void fadePointer(); |
| 936 | |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 937 | virtual std::optional<int32_t> getAssociatedDisplay(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 938 | private: |
| 939 | // Amount that trackball needs to move in order to generate a key event. |
| 940 | static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6; |
| 941 | |
| 942 | // Immutable configuration parameters. |
| 943 | struct Parameters { |
| 944 | enum Mode { |
| 945 | MODE_POINTER, |
Vladislav Kaznacheev | 78f97b3 | 2016-12-15 18:14:58 -0800 | [diff] [blame] | 946 | MODE_POINTER_RELATIVE, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 947 | MODE_NAVIGATION, |
| 948 | }; |
| 949 | |
| 950 | Mode mode; |
| 951 | bool hasAssociatedDisplay; |
| 952 | bool orientationAware; |
| 953 | } mParameters; |
| 954 | |
| 955 | CursorButtonAccumulator mCursorButtonAccumulator; |
| 956 | CursorMotionAccumulator mCursorMotionAccumulator; |
| 957 | CursorScrollAccumulator mCursorScrollAccumulator; |
| 958 | |
| 959 | int32_t mSource; |
| 960 | float mXScale; |
| 961 | float mYScale; |
| 962 | float mXPrecision; |
| 963 | float mYPrecision; |
| 964 | |
| 965 | float mVWheelScale; |
| 966 | float mHWheelScale; |
| 967 | |
| 968 | // Velocity controls for mouse pointer and wheel movements. |
| 969 | // The controls for X and Y wheel movements are separate to keep them decoupled. |
| 970 | VelocityControl mPointerVelocityControl; |
| 971 | VelocityControl mWheelXVelocityControl; |
| 972 | VelocityControl mWheelYVelocityControl; |
| 973 | |
| 974 | int32_t mOrientation; |
| 975 | |
| 976 | sp<PointerControllerInterface> mPointerController; |
| 977 | |
| 978 | int32_t mButtonState; |
| 979 | nsecs_t mDownTime; |
| 980 | |
| 981 | void configureParameters(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 982 | void dumpParameters(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 983 | |
| 984 | void sync(nsecs_t when); |
| 985 | }; |
| 986 | |
| 987 | |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 988 | class RotaryEncoderInputMapper : public InputMapper { |
| 989 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 990 | explicit RotaryEncoderInputMapper(InputDevice* device); |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 991 | virtual ~RotaryEncoderInputMapper(); |
| 992 | |
| 993 | virtual uint32_t getSources(); |
| 994 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 995 | virtual void dump(std::string& dump); |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 996 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 997 | virtual void reset(nsecs_t when); |
| 998 | virtual void process(const RawEvent* rawEvent); |
| 999 | |
| 1000 | private: |
| 1001 | CursorScrollAccumulator mRotaryEncoderScrollAccumulator; |
| 1002 | |
| 1003 | int32_t mSource; |
Prashant Malani | dae627a | 2016-01-11 17:08:18 -0800 | [diff] [blame] | 1004 | float mScalingFactor; |
Ivan Podogov | ad43725 | 2016-09-29 16:29:55 +0100 | [diff] [blame] | 1005 | int32_t mOrientation; |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 1006 | |
| 1007 | void sync(nsecs_t when); |
| 1008 | }; |
| 1009 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1010 | class TouchInputMapper : public InputMapper { |
| 1011 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 1012 | explicit TouchInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1013 | virtual ~TouchInputMapper(); |
| 1014 | |
| 1015 | virtual uint32_t getSources(); |
| 1016 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1017 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 1019 | virtual void reset(nsecs_t when); |
| 1020 | virtual void process(const RawEvent* rawEvent); |
| 1021 | |
| 1022 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 1023 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 1024 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1025 | const int32_t* keyCodes, uint8_t* outFlags); |
| 1026 | |
| 1027 | virtual void fadePointer(); |
Jeff Brown | c9aa628 | 2015-02-11 19:03:28 -0800 | [diff] [blame] | 1028 | virtual void cancelTouch(nsecs_t when); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1029 | virtual void timeoutExpired(nsecs_t when); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1030 | virtual void updateExternalStylusState(const StylusState& state); |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 1031 | virtual std::optional<int32_t> getAssociatedDisplay(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | protected: |
| 1033 | CursorButtonAccumulator mCursorButtonAccumulator; |
| 1034 | CursorScrollAccumulator mCursorScrollAccumulator; |
| 1035 | TouchButtonAccumulator mTouchButtonAccumulator; |
| 1036 | |
| 1037 | struct VirtualKey { |
| 1038 | int32_t keyCode; |
| 1039 | int32_t scanCode; |
| 1040 | uint32_t flags; |
| 1041 | |
| 1042 | // computed hit box, specified in touch screen coords based on known display size |
| 1043 | int32_t hitLeft; |
| 1044 | int32_t hitTop; |
| 1045 | int32_t hitRight; |
| 1046 | int32_t hitBottom; |
| 1047 | |
| 1048 | inline bool isHit(int32_t x, int32_t y) const { |
| 1049 | return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom; |
| 1050 | } |
| 1051 | }; |
| 1052 | |
| 1053 | // Input sources and device mode. |
| 1054 | uint32_t mSource; |
| 1055 | |
| 1056 | enum DeviceMode { |
| 1057 | DEVICE_MODE_DISABLED, // input is disabled |
| 1058 | DEVICE_MODE_DIRECT, // direct mapping (touchscreen) |
| 1059 | DEVICE_MODE_UNSCALED, // unscaled mapping (touchpad) |
| 1060 | DEVICE_MODE_NAVIGATION, // unscaled mapping with assist gesture (touch navigation) |
| 1061 | DEVICE_MODE_POINTER, // pointer mapping (pointer) |
| 1062 | }; |
| 1063 | DeviceMode mDeviceMode; |
| 1064 | |
| 1065 | // The reader's configuration. |
| 1066 | InputReaderConfiguration mConfig; |
| 1067 | |
| 1068 | // Immutable configuration parameters. |
| 1069 | struct Parameters { |
| 1070 | enum DeviceType { |
| 1071 | DEVICE_TYPE_TOUCH_SCREEN, |
| 1072 | DEVICE_TYPE_TOUCH_PAD, |
| 1073 | DEVICE_TYPE_TOUCH_NAVIGATION, |
| 1074 | DEVICE_TYPE_POINTER, |
| 1075 | }; |
| 1076 | |
| 1077 | DeviceType deviceType; |
| 1078 | bool hasAssociatedDisplay; |
| 1079 | bool associatedDisplayIsExternal; |
| 1080 | bool orientationAware; |
| 1081 | bool hasButtonUnderPad; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1082 | std::string uniqueDisplayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1083 | |
| 1084 | enum GestureMode { |
Amirhossein Simjour | 3dd617b | 2015-10-09 10:39:48 -0400 | [diff] [blame] | 1085 | GESTURE_MODE_SINGLE_TOUCH, |
| 1086 | GESTURE_MODE_MULTI_TOUCH, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1087 | }; |
| 1088 | GestureMode gestureMode; |
Jeff Brown | c5e2442 | 2014-02-26 18:48:51 -0800 | [diff] [blame] | 1089 | |
| 1090 | bool wake; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1091 | } mParameters; |
| 1092 | |
| 1093 | // Immutable calibration parameters in parsed form. |
| 1094 | struct Calibration { |
| 1095 | // Size |
| 1096 | enum SizeCalibration { |
| 1097 | SIZE_CALIBRATION_DEFAULT, |
| 1098 | SIZE_CALIBRATION_NONE, |
| 1099 | SIZE_CALIBRATION_GEOMETRIC, |
| 1100 | SIZE_CALIBRATION_DIAMETER, |
| 1101 | SIZE_CALIBRATION_BOX, |
| 1102 | SIZE_CALIBRATION_AREA, |
| 1103 | }; |
| 1104 | |
| 1105 | SizeCalibration sizeCalibration; |
| 1106 | |
| 1107 | bool haveSizeScale; |
| 1108 | float sizeScale; |
| 1109 | bool haveSizeBias; |
| 1110 | float sizeBias; |
| 1111 | bool haveSizeIsSummed; |
| 1112 | bool sizeIsSummed; |
| 1113 | |
| 1114 | // Pressure |
| 1115 | enum PressureCalibration { |
| 1116 | PRESSURE_CALIBRATION_DEFAULT, |
| 1117 | PRESSURE_CALIBRATION_NONE, |
| 1118 | PRESSURE_CALIBRATION_PHYSICAL, |
| 1119 | PRESSURE_CALIBRATION_AMPLITUDE, |
| 1120 | }; |
| 1121 | |
| 1122 | PressureCalibration pressureCalibration; |
| 1123 | bool havePressureScale; |
| 1124 | float pressureScale; |
| 1125 | |
| 1126 | // Orientation |
| 1127 | enum OrientationCalibration { |
| 1128 | ORIENTATION_CALIBRATION_DEFAULT, |
| 1129 | ORIENTATION_CALIBRATION_NONE, |
| 1130 | ORIENTATION_CALIBRATION_INTERPOLATED, |
| 1131 | ORIENTATION_CALIBRATION_VECTOR, |
| 1132 | }; |
| 1133 | |
| 1134 | OrientationCalibration orientationCalibration; |
| 1135 | |
| 1136 | // Distance |
| 1137 | enum DistanceCalibration { |
| 1138 | DISTANCE_CALIBRATION_DEFAULT, |
| 1139 | DISTANCE_CALIBRATION_NONE, |
| 1140 | DISTANCE_CALIBRATION_SCALED, |
| 1141 | }; |
| 1142 | |
| 1143 | DistanceCalibration distanceCalibration; |
| 1144 | bool haveDistanceScale; |
| 1145 | float distanceScale; |
| 1146 | |
| 1147 | enum CoverageCalibration { |
| 1148 | COVERAGE_CALIBRATION_DEFAULT, |
| 1149 | COVERAGE_CALIBRATION_NONE, |
| 1150 | COVERAGE_CALIBRATION_BOX, |
| 1151 | }; |
| 1152 | |
| 1153 | CoverageCalibration coverageCalibration; |
| 1154 | |
| 1155 | inline void applySizeScaleAndBias(float* outSize) const { |
| 1156 | if (haveSizeScale) { |
| 1157 | *outSize *= sizeScale; |
| 1158 | } |
| 1159 | if (haveSizeBias) { |
| 1160 | *outSize += sizeBias; |
| 1161 | } |
| 1162 | if (*outSize < 0) { |
| 1163 | *outSize = 0; |
| 1164 | } |
| 1165 | } |
| 1166 | } mCalibration; |
| 1167 | |
Jason Gerecke | af126fb | 2012-05-10 14:22:47 -0700 | [diff] [blame] | 1168 | // Affine location transformation/calibration |
| 1169 | struct TouchAffineTransformation mAffineTransform; |
| 1170 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | RawPointerAxes mRawPointerAxes; |
| 1172 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1173 | struct RawState { |
| 1174 | nsecs_t when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1176 | // Raw pointer sample data. |
| 1177 | RawPointerData rawPointerData; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1179 | int32_t buttonState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1181 | // Scroll state. |
| 1182 | int32_t rawVScroll; |
| 1183 | int32_t rawHScroll; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1185 | void copyFrom(const RawState& other) { |
| 1186 | when = other.when; |
| 1187 | rawPointerData.copyFrom(other.rawPointerData); |
| 1188 | buttonState = other.buttonState; |
| 1189 | rawVScroll = other.rawVScroll; |
| 1190 | rawHScroll = other.rawHScroll; |
| 1191 | } |
| 1192 | |
| 1193 | void clear() { |
| 1194 | when = 0; |
| 1195 | rawPointerData.clear(); |
| 1196 | buttonState = 0; |
| 1197 | rawVScroll = 0; |
| 1198 | rawHScroll = 0; |
| 1199 | } |
| 1200 | }; |
| 1201 | |
| 1202 | struct CookedState { |
| 1203 | // Cooked pointer sample data. |
| 1204 | CookedPointerData cookedPointerData; |
| 1205 | |
| 1206 | // Id bits used to differentiate fingers, stylus and mouse tools. |
| 1207 | BitSet32 fingerIdBits; |
| 1208 | BitSet32 stylusIdBits; |
| 1209 | BitSet32 mouseIdBits; |
| 1210 | |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 1211 | int32_t buttonState; |
| 1212 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1213 | void copyFrom(const CookedState& other) { |
| 1214 | cookedPointerData.copyFrom(other.cookedPointerData); |
| 1215 | fingerIdBits = other.fingerIdBits; |
| 1216 | stylusIdBits = other.stylusIdBits; |
| 1217 | mouseIdBits = other.mouseIdBits; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 1218 | buttonState = other.buttonState; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | void clear() { |
| 1222 | cookedPointerData.clear(); |
| 1223 | fingerIdBits.clear(); |
| 1224 | stylusIdBits.clear(); |
| 1225 | mouseIdBits.clear(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 1226 | buttonState = 0; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1227 | } |
| 1228 | }; |
| 1229 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1230 | std::vector<RawState> mRawStatesPending; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1231 | RawState mCurrentRawState; |
| 1232 | CookedState mCurrentCookedState; |
| 1233 | RawState mLastRawState; |
| 1234 | CookedState mLastCookedState; |
| 1235 | |
| 1236 | // State provided by an external stylus |
| 1237 | StylusState mExternalStylusState; |
| 1238 | int64_t mExternalStylusId; |
Michael Wright | 43fd19f | 2015-04-21 19:02:58 +0100 | [diff] [blame] | 1239 | nsecs_t mExternalStylusFusionTimeout; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1240 | bool mExternalStylusDataPending; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | |
| 1242 | // True if we sent a HOVER_ENTER event. |
| 1243 | bool mSentHoverEnter; |
| 1244 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1245 | // Have we assigned pointer IDs for this stream |
| 1246 | bool mHavePointerIds; |
| 1247 | |
Michael Wright | 8e81282 | 2015-06-22 16:18:21 +0100 | [diff] [blame] | 1248 | // Is the current stream of direct touch events aborted |
| 1249 | bool mCurrentMotionAborted; |
| 1250 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1251 | // The time the primary pointer last went down. |
| 1252 | nsecs_t mDownTime; |
| 1253 | |
| 1254 | // The pointer controller, or null if the device is not a pointer. |
| 1255 | sp<PointerControllerInterface> mPointerController; |
| 1256 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1257 | std::vector<VirtualKey> mVirtualKeys; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1258 | |
| 1259 | virtual void configureParameters(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1260 | virtual void dumpParameters(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1261 | virtual void configureRawPointerAxes(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1262 | virtual void dumpRawPointerAxes(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | virtual void configureSurface(nsecs_t when, bool* outResetNeeded); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1264 | virtual void dumpSurface(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | virtual void configureVirtualKeys(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1266 | virtual void dumpVirtualKeys(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1267 | virtual void parseCalibration(); |
| 1268 | virtual void resolveCalibration(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1269 | virtual void dumpCalibration(std::string& dump); |
Jason Gerecke | 12d6baa | 2014-01-27 18:34:20 -0800 | [diff] [blame] | 1270 | virtual void updateAffineTransformation(); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1271 | virtual void dumpAffineTransformation(std::string& dump); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1272 | virtual void resolveExternalStylusPresence(); |
| 1273 | virtual bool hasStylus() const = 0; |
| 1274 | virtual bool hasExternalStylus() const; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1275 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1276 | virtual void syncTouch(nsecs_t when, RawState* outState) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | |
| 1278 | private: |
| 1279 | // The current viewport. |
| 1280 | // The components of the viewport are specified in the display's rotated orientation. |
| 1281 | DisplayViewport mViewport; |
| 1282 | |
| 1283 | // The surface orientation, width and height set by configureSurface(). |
| 1284 | // The width and height are derived from the viewport but are specified |
| 1285 | // in the natural orientation. |
| 1286 | // The surface origin specifies how the surface coordinates should be translated |
| 1287 | // to align with the logical display coordinate space. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1288 | int32_t mSurfaceWidth; |
| 1289 | int32_t mSurfaceHeight; |
| 1290 | int32_t mSurfaceLeft; |
| 1291 | int32_t mSurfaceTop; |
Michael Wright | 358bcc7 | 2018-08-21 04:01:07 +0100 | [diff] [blame] | 1292 | |
| 1293 | // Similar to the surface coordinates, but in the raw display coordinate space rather than in |
| 1294 | // the logical coordinate space. |
| 1295 | int32_t mPhysicalWidth; |
| 1296 | int32_t mPhysicalHeight; |
| 1297 | int32_t mPhysicalLeft; |
| 1298 | int32_t mPhysicalTop; |
| 1299 | |
| 1300 | // The orientation may be different from the viewport orientation as it specifies |
| 1301 | // the rotation of the surface coordinates required to produce the viewport's |
| 1302 | // requested orientation, so it will depend on whether the device is orientation aware. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | int32_t mSurfaceOrientation; |
| 1304 | |
| 1305 | // Translation and scaling factors, orientation-independent. |
| 1306 | float mXTranslate; |
| 1307 | float mXScale; |
| 1308 | float mXPrecision; |
| 1309 | |
| 1310 | float mYTranslate; |
| 1311 | float mYScale; |
| 1312 | float mYPrecision; |
| 1313 | |
| 1314 | float mGeometricScale; |
| 1315 | |
| 1316 | float mPressureScale; |
| 1317 | |
| 1318 | float mSizeScale; |
| 1319 | |
| 1320 | float mOrientationScale; |
| 1321 | |
| 1322 | float mDistanceScale; |
| 1323 | |
| 1324 | bool mHaveTilt; |
| 1325 | float mTiltXCenter; |
| 1326 | float mTiltXScale; |
| 1327 | float mTiltYCenter; |
| 1328 | float mTiltYScale; |
| 1329 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1330 | bool mExternalStylusConnected; |
| 1331 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1332 | // Oriented motion ranges for input device info. |
| 1333 | struct OrientedRanges { |
| 1334 | InputDeviceInfo::MotionRange x; |
| 1335 | InputDeviceInfo::MotionRange y; |
| 1336 | InputDeviceInfo::MotionRange pressure; |
| 1337 | |
| 1338 | bool haveSize; |
| 1339 | InputDeviceInfo::MotionRange size; |
| 1340 | |
| 1341 | bool haveTouchSize; |
| 1342 | InputDeviceInfo::MotionRange touchMajor; |
| 1343 | InputDeviceInfo::MotionRange touchMinor; |
| 1344 | |
| 1345 | bool haveToolSize; |
| 1346 | InputDeviceInfo::MotionRange toolMajor; |
| 1347 | InputDeviceInfo::MotionRange toolMinor; |
| 1348 | |
| 1349 | bool haveOrientation; |
| 1350 | InputDeviceInfo::MotionRange orientation; |
| 1351 | |
| 1352 | bool haveDistance; |
| 1353 | InputDeviceInfo::MotionRange distance; |
| 1354 | |
| 1355 | bool haveTilt; |
| 1356 | InputDeviceInfo::MotionRange tilt; |
| 1357 | |
| 1358 | OrientedRanges() { |
| 1359 | clear(); |
| 1360 | } |
| 1361 | |
| 1362 | void clear() { |
| 1363 | haveSize = false; |
| 1364 | haveTouchSize = false; |
| 1365 | haveToolSize = false; |
| 1366 | haveOrientation = false; |
| 1367 | haveDistance = false; |
| 1368 | haveTilt = false; |
| 1369 | } |
| 1370 | } mOrientedRanges; |
| 1371 | |
| 1372 | // Oriented dimensions and precision. |
| 1373 | float mOrientedXPrecision; |
| 1374 | float mOrientedYPrecision; |
| 1375 | |
| 1376 | struct CurrentVirtualKeyState { |
| 1377 | bool down; |
| 1378 | bool ignored; |
| 1379 | nsecs_t downTime; |
| 1380 | int32_t keyCode; |
| 1381 | int32_t scanCode; |
| 1382 | } mCurrentVirtualKey; |
| 1383 | |
| 1384 | // Scale factor for gesture or mouse based pointer movements. |
| 1385 | float mPointerXMovementScale; |
| 1386 | float mPointerYMovementScale; |
| 1387 | |
| 1388 | // Scale factor for gesture based zooming and other freeform motions. |
| 1389 | float mPointerXZoomScale; |
| 1390 | float mPointerYZoomScale; |
| 1391 | |
| 1392 | // The maximum swipe width. |
| 1393 | float mPointerGestureMaxSwipeWidth; |
| 1394 | |
| 1395 | struct PointerDistanceHeapElement { |
| 1396 | uint32_t currentPointerIndex : 8; |
| 1397 | uint32_t lastPointerIndex : 8; |
| 1398 | uint64_t distance : 48; // squared distance |
| 1399 | }; |
| 1400 | |
| 1401 | enum PointerUsage { |
| 1402 | POINTER_USAGE_NONE, |
| 1403 | POINTER_USAGE_GESTURES, |
| 1404 | POINTER_USAGE_STYLUS, |
| 1405 | POINTER_USAGE_MOUSE, |
| 1406 | }; |
| 1407 | PointerUsage mPointerUsage; |
| 1408 | |
| 1409 | struct PointerGesture { |
| 1410 | enum Mode { |
| 1411 | // No fingers, button is not pressed. |
| 1412 | // Nothing happening. |
| 1413 | NEUTRAL, |
| 1414 | |
| 1415 | // No fingers, button is not pressed. |
| 1416 | // Tap detected. |
| 1417 | // Emits DOWN and UP events at the pointer location. |
| 1418 | TAP, |
| 1419 | |
| 1420 | // Exactly one finger dragging following a tap. |
| 1421 | // Pointer follows the active finger. |
| 1422 | // Emits DOWN, MOVE and UP events at the pointer location. |
| 1423 | // |
| 1424 | // Detect double-taps when the finger goes up while in TAP_DRAG mode. |
| 1425 | TAP_DRAG, |
| 1426 | |
| 1427 | // Button is pressed. |
| 1428 | // Pointer follows the active finger if there is one. Other fingers are ignored. |
| 1429 | // Emits DOWN, MOVE and UP events at the pointer location. |
| 1430 | BUTTON_CLICK_OR_DRAG, |
| 1431 | |
| 1432 | // Exactly one finger, button is not pressed. |
| 1433 | // Pointer follows the active finger. |
| 1434 | // Emits HOVER_MOVE events at the pointer location. |
| 1435 | // |
| 1436 | // Detect taps when the finger goes up while in HOVER mode. |
| 1437 | HOVER, |
| 1438 | |
| 1439 | // Exactly two fingers but neither have moved enough to clearly indicate |
| 1440 | // whether a swipe or freeform gesture was intended. We consider the |
| 1441 | // pointer to be pressed so this enables clicking or long-pressing on buttons. |
| 1442 | // Pointer does not move. |
| 1443 | // Emits DOWN, MOVE and UP events with a single stationary pointer coordinate. |
| 1444 | PRESS, |
| 1445 | |
| 1446 | // Exactly two fingers moving in the same direction, button is not pressed. |
| 1447 | // Pointer does not move. |
| 1448 | // Emits DOWN, MOVE and UP events with a single pointer coordinate that |
| 1449 | // follows the midpoint between both fingers. |
| 1450 | SWIPE, |
| 1451 | |
| 1452 | // Two or more fingers moving in arbitrary directions, button is not pressed. |
| 1453 | // Pointer does not move. |
| 1454 | // Emits DOWN, POINTER_DOWN, MOVE, POINTER_UP and UP events that follow |
| 1455 | // each finger individually relative to the initial centroid of the finger. |
| 1456 | FREEFORM, |
| 1457 | |
| 1458 | // Waiting for quiet time to end before starting the next gesture. |
| 1459 | QUIET, |
| 1460 | }; |
| 1461 | |
| 1462 | // Time the first finger went down. |
| 1463 | nsecs_t firstTouchTime; |
| 1464 | |
| 1465 | // The active pointer id from the raw touch data. |
| 1466 | int32_t activeTouchId; // -1 if none |
| 1467 | |
| 1468 | // The active pointer id from the gesture last delivered to the application. |
| 1469 | int32_t activeGestureId; // -1 if none |
| 1470 | |
| 1471 | // Pointer coords and ids for the current and previous pointer gesture. |
| 1472 | Mode currentGestureMode; |
| 1473 | BitSet32 currentGestureIdBits; |
| 1474 | uint32_t currentGestureIdToIndex[MAX_POINTER_ID + 1]; |
| 1475 | PointerProperties currentGestureProperties[MAX_POINTERS]; |
| 1476 | PointerCoords currentGestureCoords[MAX_POINTERS]; |
| 1477 | |
| 1478 | Mode lastGestureMode; |
| 1479 | BitSet32 lastGestureIdBits; |
| 1480 | uint32_t lastGestureIdToIndex[MAX_POINTER_ID + 1]; |
| 1481 | PointerProperties lastGestureProperties[MAX_POINTERS]; |
| 1482 | PointerCoords lastGestureCoords[MAX_POINTERS]; |
| 1483 | |
| 1484 | // Time the pointer gesture last went down. |
| 1485 | nsecs_t downTime; |
| 1486 | |
| 1487 | // Time when the pointer went down for a TAP. |
| 1488 | nsecs_t tapDownTime; |
| 1489 | |
| 1490 | // Time when the pointer went up for a TAP. |
| 1491 | nsecs_t tapUpTime; |
| 1492 | |
| 1493 | // Location of initial tap. |
| 1494 | float tapX, tapY; |
| 1495 | |
| 1496 | // Time we started waiting for quiescence. |
| 1497 | nsecs_t quietTime; |
| 1498 | |
| 1499 | // Reference points for multitouch gestures. |
| 1500 | float referenceTouchX; // reference touch X/Y coordinates in surface units |
| 1501 | float referenceTouchY; |
| 1502 | float referenceGestureX; // reference gesture X/Y coordinates in pixels |
| 1503 | float referenceGestureY; |
| 1504 | |
| 1505 | // Distance that each pointer has traveled which has not yet been |
| 1506 | // subsumed into the reference gesture position. |
| 1507 | BitSet32 referenceIdBits; |
| 1508 | struct Delta { |
| 1509 | float dx, dy; |
| 1510 | }; |
| 1511 | Delta referenceDeltas[MAX_POINTER_ID + 1]; |
| 1512 | |
| 1513 | // Describes how touch ids are mapped to gesture ids for freeform gestures. |
| 1514 | uint32_t freeformTouchToGestureIdMap[MAX_POINTER_ID + 1]; |
| 1515 | |
| 1516 | // A velocity tracker for determining whether to switch active pointers during drags. |
| 1517 | VelocityTracker velocityTracker; |
| 1518 | |
| 1519 | void reset() { |
| 1520 | firstTouchTime = LLONG_MIN; |
| 1521 | activeTouchId = -1; |
| 1522 | activeGestureId = -1; |
| 1523 | currentGestureMode = NEUTRAL; |
| 1524 | currentGestureIdBits.clear(); |
| 1525 | lastGestureMode = NEUTRAL; |
| 1526 | lastGestureIdBits.clear(); |
| 1527 | downTime = 0; |
| 1528 | velocityTracker.clear(); |
| 1529 | resetTap(); |
| 1530 | resetQuietTime(); |
| 1531 | } |
| 1532 | |
| 1533 | void resetTap() { |
| 1534 | tapDownTime = LLONG_MIN; |
| 1535 | tapUpTime = LLONG_MIN; |
| 1536 | } |
| 1537 | |
| 1538 | void resetQuietTime() { |
| 1539 | quietTime = LLONG_MIN; |
| 1540 | } |
| 1541 | } mPointerGesture; |
| 1542 | |
| 1543 | struct PointerSimple { |
| 1544 | PointerCoords currentCoords; |
| 1545 | PointerProperties currentProperties; |
| 1546 | PointerCoords lastCoords; |
| 1547 | PointerProperties lastProperties; |
| 1548 | |
| 1549 | // True if the pointer is down. |
| 1550 | bool down; |
| 1551 | |
| 1552 | // True if the pointer is hovering. |
| 1553 | bool hovering; |
| 1554 | |
| 1555 | // Time the pointer last went down. |
| 1556 | nsecs_t downTime; |
| 1557 | |
| 1558 | void reset() { |
| 1559 | currentCoords.clear(); |
| 1560 | currentProperties.clear(); |
| 1561 | lastCoords.clear(); |
| 1562 | lastProperties.clear(); |
| 1563 | down = false; |
| 1564 | hovering = false; |
| 1565 | downTime = 0; |
| 1566 | } |
| 1567 | } mPointerSimple; |
| 1568 | |
| 1569 | // The pointer and scroll velocity controls. |
| 1570 | VelocityControl mPointerVelocityControl; |
| 1571 | VelocityControl mWheelXVelocityControl; |
| 1572 | VelocityControl mWheelYVelocityControl; |
| 1573 | |
Siarhei Vishniakou | 9ffab0c | 2018-11-08 19:54:22 -0800 | [diff] [blame] | 1574 | // Latency statistics for touch events |
| 1575 | struct LatencyStatistics mStatistics; |
| 1576 | |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 1577 | std::optional<DisplayViewport> findViewport(); |
| 1578 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1579 | void resetExternalStylus(); |
Michael Wright | 43fd19f | 2015-04-21 19:02:58 +0100 | [diff] [blame] | 1580 | void clearStylusDataPendingFlags(); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1581 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1582 | void sync(nsecs_t when); |
| 1583 | |
| 1584 | bool consumeRawTouches(nsecs_t when, uint32_t policyFlags); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1585 | void processRawTouches(bool timeout); |
| 1586 | void cookAndDispatch(nsecs_t when); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | void dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 1588 | int32_t keyEventAction, int32_t keyEventFlags); |
| 1589 | |
| 1590 | void dispatchTouches(nsecs_t when, uint32_t policyFlags); |
| 1591 | void dispatchHoverExit(nsecs_t when, uint32_t policyFlags); |
| 1592 | void dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 1593 | void dispatchButtonRelease(nsecs_t when, uint32_t policyFlags); |
| 1594 | void dispatchButtonPress(nsecs_t when, uint32_t policyFlags); |
| 1595 | const BitSet32& findActiveIdBits(const CookedPointerData& cookedPointerData); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1596 | void cookPointerData(); |
Michael Wright | 8e81282 | 2015-06-22 16:18:21 +0100 | [diff] [blame] | 1597 | void abortTouches(nsecs_t when, uint32_t policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1598 | |
| 1599 | void dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, PointerUsage pointerUsage); |
| 1600 | void abortPointerUsage(nsecs_t when, uint32_t policyFlags); |
| 1601 | |
| 1602 | void dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, bool isTimeout); |
| 1603 | void abortPointerGestures(nsecs_t when, uint32_t policyFlags); |
| 1604 | bool preparePointerGestures(nsecs_t when, |
| 1605 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, |
| 1606 | bool isTimeout); |
| 1607 | |
| 1608 | void dispatchPointerStylus(nsecs_t when, uint32_t policyFlags); |
| 1609 | void abortPointerStylus(nsecs_t when, uint32_t policyFlags); |
| 1610 | |
| 1611 | void dispatchPointerMouse(nsecs_t when, uint32_t policyFlags); |
| 1612 | void abortPointerMouse(nsecs_t when, uint32_t policyFlags); |
| 1613 | |
| 1614 | void dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 1615 | bool down, bool hovering); |
| 1616 | void abortPointerSimple(nsecs_t when, uint32_t policyFlags); |
| 1617 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1618 | bool assignExternalStylusId(const RawState& state, bool timeout); |
| 1619 | void applyExternalStylusButtonState(nsecs_t when); |
| 1620 | void applyExternalStylusTouchState(nsecs_t when); |
| 1621 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | // Dispatches a motion event. |
| 1623 | // If the changedId is >= 0 and the action is POINTER_DOWN or POINTER_UP, the |
| 1624 | // method will take care of setting the index and transmuting the action to DOWN or UP |
| 1625 | // it is the first / last pointer to go down / up. |
| 1626 | void dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 1627 | int32_t action, int32_t actionButton, |
| 1628 | int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1629 | const PointerProperties* properties, const PointerCoords* coords, |
| 1630 | const uint32_t* idToIndex, BitSet32 idBits, |
| 1631 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime); |
| 1632 | |
| 1633 | // Updates pointer coords and properties for pointers with specified ids that have moved. |
| 1634 | // Returns true if any of them changed. |
| 1635 | bool updateMovedPointers(const PointerProperties* inProperties, |
| 1636 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
| 1637 | PointerProperties* outProperties, PointerCoords* outCoords, |
| 1638 | const uint32_t* outIdToIndex, BitSet32 idBits) const; |
| 1639 | |
| 1640 | bool isPointInsideSurface(int32_t x, int32_t y); |
| 1641 | const VirtualKey* findVirtualKeyHit(int32_t x, int32_t y); |
| 1642 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1643 | static void assignPointerIds(const RawState* last, RawState* current); |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 1644 | |
Siarhei Vishniakou | 9ffab0c | 2018-11-08 19:54:22 -0800 | [diff] [blame] | 1645 | void reportEventForStatistics(nsecs_t evdevTime); |
| 1646 | |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 1647 | const char* modeToString(DeviceMode deviceMode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1648 | }; |
| 1649 | |
| 1650 | |
| 1651 | class SingleTouchInputMapper : public TouchInputMapper { |
| 1652 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 1653 | explicit SingleTouchInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1654 | virtual ~SingleTouchInputMapper(); |
| 1655 | |
| 1656 | virtual void reset(nsecs_t when); |
| 1657 | virtual void process(const RawEvent* rawEvent); |
| 1658 | |
| 1659 | protected: |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1660 | virtual void syncTouch(nsecs_t when, RawState* outState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1661 | virtual void configureRawPointerAxes(); |
| 1662 | virtual bool hasStylus() const; |
| 1663 | |
| 1664 | private: |
| 1665 | SingleTouchMotionAccumulator mSingleTouchMotionAccumulator; |
| 1666 | }; |
| 1667 | |
| 1668 | |
| 1669 | class MultiTouchInputMapper : public TouchInputMapper { |
| 1670 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 1671 | explicit MultiTouchInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1672 | virtual ~MultiTouchInputMapper(); |
| 1673 | |
| 1674 | virtual void reset(nsecs_t when); |
| 1675 | virtual void process(const RawEvent* rawEvent); |
| 1676 | |
| 1677 | protected: |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1678 | virtual void syncTouch(nsecs_t when, RawState* outState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1679 | virtual void configureRawPointerAxes(); |
| 1680 | virtual bool hasStylus() const; |
| 1681 | |
| 1682 | private: |
| 1683 | MultiTouchMotionAccumulator mMultiTouchMotionAccumulator; |
| 1684 | |
| 1685 | // Specifies the pointer id bits that are in use, and their associated tracking id. |
| 1686 | BitSet32 mPointerIdBits; |
| 1687 | int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1]; |
| 1688 | }; |
| 1689 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1690 | class ExternalStylusInputMapper : public InputMapper { |
| 1691 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 1692 | explicit ExternalStylusInputMapper(InputDevice* device); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1693 | virtual ~ExternalStylusInputMapper() = default; |
| 1694 | |
| 1695 | virtual uint32_t getSources(); |
| 1696 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1697 | virtual void dump(std::string& dump); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1698 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 1699 | virtual void reset(nsecs_t when); |
| 1700 | virtual void process(const RawEvent* rawEvent); |
| 1701 | virtual void sync(nsecs_t when); |
| 1702 | |
| 1703 | private: |
| 1704 | SingleTouchMotionAccumulator mSingleTouchMotionAccumulator; |
| 1705 | RawAbsoluteAxisInfo mRawPressureAxis; |
| 1706 | TouchButtonAccumulator mTouchButtonAccumulator; |
| 1707 | |
| 1708 | StylusState mStylusState; |
| 1709 | }; |
| 1710 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1711 | |
| 1712 | class JoystickInputMapper : public InputMapper { |
| 1713 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 1714 | explicit JoystickInputMapper(InputDevice* device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1715 | virtual ~JoystickInputMapper(); |
| 1716 | |
| 1717 | virtual uint32_t getSources(); |
| 1718 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1719 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 1721 | virtual void reset(nsecs_t when); |
| 1722 | virtual void process(const RawEvent* rawEvent); |
| 1723 | |
| 1724 | private: |
| 1725 | struct Axis { |
| 1726 | RawAbsoluteAxisInfo rawAxisInfo; |
| 1727 | AxisInfo axisInfo; |
| 1728 | |
| 1729 | bool explicitlyMapped; // true if the axis was explicitly assigned an axis id |
| 1730 | |
| 1731 | float scale; // scale factor from raw to normalized values |
| 1732 | float offset; // offset to add after scaling for normalization |
| 1733 | float highScale; // scale factor from raw to normalized values of high split |
| 1734 | float highOffset; // offset to add after scaling for normalization of high split |
| 1735 | |
| 1736 | float min; // normalized inclusive minimum |
| 1737 | float max; // normalized inclusive maximum |
| 1738 | float flat; // normalized flat region size |
| 1739 | float fuzz; // normalized error tolerance |
| 1740 | float resolution; // normalized resolution in units/mm |
| 1741 | |
| 1742 | float filter; // filter out small variations of this size |
| 1743 | float currentValue; // current value |
| 1744 | float newValue; // most recent value |
| 1745 | float highCurrentValue; // current value of high split |
| 1746 | float highNewValue; // most recent value of high split |
| 1747 | |
| 1748 | void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo, |
| 1749 | bool explicitlyMapped, float scale, float offset, |
| 1750 | float highScale, float highOffset, |
| 1751 | float min, float max, float flat, float fuzz, float resolution) { |
| 1752 | this->rawAxisInfo = rawAxisInfo; |
| 1753 | this->axisInfo = axisInfo; |
| 1754 | this->explicitlyMapped = explicitlyMapped; |
| 1755 | this->scale = scale; |
| 1756 | this->offset = offset; |
| 1757 | this->highScale = highScale; |
| 1758 | this->highOffset = highOffset; |
| 1759 | this->min = min; |
| 1760 | this->max = max; |
| 1761 | this->flat = flat; |
| 1762 | this->fuzz = fuzz; |
| 1763 | this->resolution = resolution; |
| 1764 | this->filter = 0; |
| 1765 | resetValue(); |
| 1766 | } |
| 1767 | |
| 1768 | void resetValue() { |
| 1769 | this->currentValue = 0; |
| 1770 | this->newValue = 0; |
| 1771 | this->highCurrentValue = 0; |
| 1772 | this->highNewValue = 0; |
| 1773 | } |
| 1774 | }; |
| 1775 | |
| 1776 | // Axes indexed by raw ABS_* axis index. |
| 1777 | KeyedVector<int32_t, Axis> mAxes; |
| 1778 | |
| 1779 | void sync(nsecs_t when, bool force); |
| 1780 | |
| 1781 | bool haveAxis(int32_t axisId); |
| 1782 | void pruneAxes(bool ignoreExplicitlyMappedAxes); |
| 1783 | bool filterAxes(bool force); |
| 1784 | |
| 1785 | static bool hasValueChangedSignificantly(float filter, |
| 1786 | float newValue, float currentValue, float min, float max); |
| 1787 | static bool hasMovedNearerToValueWithinFilteredRange(float filter, |
| 1788 | float newValue, float currentValue, float thresholdValue); |
| 1789 | |
| 1790 | static bool isCenteredAxis(int32_t axis); |
| 1791 | static int32_t getCompatAxis(int32_t axis); |
| 1792 | |
| 1793 | static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info); |
| 1794 | static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, |
| 1795 | float value); |
| 1796 | }; |
| 1797 | |
| 1798 | } // namespace android |
| 1799 | |
| 1800 | #endif // _UI_INPUT_READER_H |