Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _UI_INPUTREADER_MULTI_TOUCH_INPUT_MAPPER_H |
| 18 | #define _UI_INPUTREADER_MULTI_TOUCH_INPUT_MAPPER_H |
| 19 | |
| 20 | #include "TouchInputMapper.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | /* Keeps track of the state of multi-touch protocol. */ |
| 25 | class MultiTouchMotionAccumulator { |
| 26 | public: |
| 27 | class Slot { |
| 28 | public: |
| 29 | inline bool isInUse() const { return mInUse; } |
| 30 | inline int32_t getX() const { return mAbsMTPositionX; } |
| 31 | inline int32_t getY() const { return mAbsMTPositionY; } |
| 32 | inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; } |
| 33 | inline int32_t getTouchMinor() const { |
| 34 | return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor; |
| 35 | } |
| 36 | inline int32_t getToolMajor() const { return mAbsMTWidthMajor; } |
| 37 | inline int32_t getToolMinor() const { |
| 38 | return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor; |
| 39 | } |
| 40 | inline int32_t getOrientation() const { return mAbsMTOrientation; } |
| 41 | inline int32_t getTrackingId() const { return mAbsMTTrackingId; } |
| 42 | inline int32_t getPressure() const { return mAbsMTPressure; } |
| 43 | inline int32_t getDistance() const { return mAbsMTDistance; } |
| 44 | inline int32_t getToolType() const; |
| 45 | |
| 46 | private: |
| 47 | friend class MultiTouchMotionAccumulator; |
| 48 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 49 | bool mInUse = false; |
| 50 | bool mHaveAbsMTTouchMinor = false; |
| 51 | bool mHaveAbsMTWidthMinor = false; |
| 52 | bool mHaveAbsMTToolType = false; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 53 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 54 | int32_t mAbsMTPositionX = 0; |
| 55 | int32_t mAbsMTPositionY = 0; |
| 56 | int32_t mAbsMTTouchMajor = 0; |
| 57 | int32_t mAbsMTTouchMinor = 0; |
| 58 | int32_t mAbsMTWidthMajor = 0; |
| 59 | int32_t mAbsMTWidthMinor = 0; |
| 60 | int32_t mAbsMTOrientation = 0; |
| 61 | int32_t mAbsMTTrackingId = -1; |
| 62 | int32_t mAbsMTPressure = 0; |
| 63 | int32_t mAbsMTDistance = 0; |
| 64 | int32_t mAbsMTToolType = 0; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 65 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 66 | void clear() { *this = Slot(); } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | MultiTouchMotionAccumulator(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 70 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 71 | void configure(InputDeviceContext& deviceContext, size_t slotCount, bool usingSlotsProtocol); |
| 72 | void reset(InputDeviceContext& deviceContext); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 73 | void process(const RawEvent* rawEvent); |
| 74 | void finishSync(); |
| 75 | bool hasStylus() const; |
| 76 | |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 77 | inline size_t getSlotCount() const { return mSlots.size(); } |
| 78 | inline const Slot& getSlot(size_t index) const { |
| 79 | LOG_ALWAYS_FATAL_IF(index < 0 || index >= mSlots.size(), "Invalid index: %zu", index); |
| 80 | return mSlots[index]; |
| 81 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | int32_t mCurrentSlot; |
Siarhei Vishniakou | baf0b16 | 2022-02-16 11:12:36 -0800 | [diff] [blame] | 85 | std::vector<Slot> mSlots; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 86 | bool mUsingSlotsProtocol; |
| 87 | bool mHaveStylus; |
| 88 | |
| 89 | void clearSlots(int32_t initialSlot); |
Arthur Hung | 9ad1894 | 2021-06-19 02:04:46 +0000 | [diff] [blame] | 90 | void warnIfNotInUse(const RawEvent& event, const Slot& slot); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | class MultiTouchInputMapper : public TouchInputMapper { |
| 94 | public: |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 95 | explicit MultiTouchInputMapper(InputDeviceContext& deviceContext); |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 96 | ~MultiTouchInputMapper() override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 97 | |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 98 | void reset(nsecs_t when) override; |
| 99 | void process(const RawEvent* rawEvent) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 100 | |
| 101 | protected: |
Michael Wright | 227c554 | 2020-07-02 18:30:52 +0100 | [diff] [blame] | 102 | void syncTouch(nsecs_t when, RawState* outState) override; |
| 103 | void configureRawPointerAxes() override; |
| 104 | bool hasStylus() const override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 105 | |
| 106 | private: |
Prabir Pradhan | f4d65b1 | 2022-02-10 07:15:38 -0800 | [diff] [blame] | 107 | // simulate_stylus_with_touch is a debug mode that converts all finger pointers reported by this |
| 108 | // mapper's touchscreen into stylus pointers, and adds SOURCE_STYLUS to the input device. |
| 109 | // It is used to simulate stylus events for debugging and testing on a device that does not |
| 110 | // support styluses. It can be enabled using |
| 111 | // "adb shell setprop persist.debug.input.simulate_stylus_with_touch true", |
| 112 | // and requires a reboot to take effect. |
| 113 | inline bool shouldSimulateStylusWithTouch() const; |
| 114 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 115 | // If the slot is in use, return the bit id. Return std::nullopt otherwise. |
| 116 | std::optional<int32_t> getActiveBitId(const MultiTouchMotionAccumulator::Slot& inSlot); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 117 | MultiTouchMotionAccumulator mMultiTouchMotionAccumulator; |
| 118 | |
| 119 | // Specifies the pointer id bits that are in use, and their associated tracking id. |
| 120 | BitSet32 mPointerIdBits; |
| 121 | int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1]; |
| 122 | }; |
| 123 | |
| 124 | } // namespace android |
| 125 | |
| 126 | #endif // _UI_INPUTREADER_MULTI_TOUCH_INPUT_MAPPER_H |