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