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:
isInUse()29         inline bool isInUse() const { return mInUse; }
getX()30         inline int32_t getX() const { return mAbsMTPositionX; }
getY()31         inline int32_t getY() const { return mAbsMTPositionY; }
getTouchMajor()32         inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
getTouchMinor()33         inline int32_t getTouchMinor() const {
34             return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor;
35         }
getToolMajor()36         inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
getToolMinor()37         inline int32_t getToolMinor() const {
38             return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor;
39         }
getOrientation()40         inline int32_t getOrientation() const { return mAbsMTOrientation; }
getTrackingId()41         inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
getPressure()42         inline int32_t getPressure() const { return mAbsMTPressure; }
getDistance()43         inline int32_t getDistance() const { return mAbsMTDistance; }
44         inline int32_t getToolType() const;
45 
46     private:
47         friend class MultiTouchMotionAccumulator;
48 
49         bool mInUse;
50         bool mHaveAbsMTTouchMinor;
51         bool mHaveAbsMTWidthMinor;
52         bool mHaveAbsMTToolType;
53 
54         int32_t mAbsMTPositionX;
55         int32_t mAbsMTPositionY;
56         int32_t mAbsMTTouchMajor;
57         int32_t mAbsMTTouchMinor;
58         int32_t mAbsMTWidthMajor;
59         int32_t mAbsMTWidthMinor;
60         int32_t mAbsMTOrientation;
61         int32_t mAbsMTTrackingId;
62         int32_t mAbsMTPressure;
63         int32_t mAbsMTDistance;
64         int32_t mAbsMTToolType;
65 
66         Slot();
67         void clear();
68     };
69 
70     MultiTouchMotionAccumulator();
71     ~MultiTouchMotionAccumulator();
72 
73     void configure(InputDeviceContext& deviceContext, size_t slotCount, bool usingSlotsProtocol);
74     void reset(InputDeviceContext& deviceContext);
75     void process(const RawEvent* rawEvent);
76     void finishSync();
77     bool hasStylus() const;
78 
getSlotCount()79     inline size_t getSlotCount() const { return mSlotCount; }
getSlot(size_t index)80     inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
81 
82 private:
83     int32_t mCurrentSlot;
84     Slot* mSlots;
85     size_t mSlotCount;
86     bool mUsingSlotsProtocol;
87     bool mHaveStylus;
88 
89     void clearSlots(int32_t initialSlot);
90 };
91 
92 class MultiTouchInputMapper : public TouchInputMapper {
93 public:
94     explicit MultiTouchInputMapper(InputDeviceContext& deviceContext);
95     virtual ~MultiTouchInputMapper();
96 
97     virtual void reset(nsecs_t when) override;
98     virtual void process(const RawEvent* rawEvent) override;
99 
100 protected:
101     virtual void syncTouch(nsecs_t when, RawState* outState);
102     virtual void configureRawPointerAxes();
103     virtual bool hasStylus() const;
104 
105 private:
106     MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
107 
108     // Specifies the pointer id bits that are in use, and their associated tracking id.
109     BitSet32 mPointerIdBits;
110     int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
111 };
112 
113 } // namespace android
114 
115 #endif // _UI_INPUTREADER_MULTI_TOUCH_INPUT_MAPPER_H