• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 // clang-format off
18 #include "../Macros.h"
19 // clang-format on
20 #include "MultiTouchMotionAccumulator.h"
21 
22 namespace android {
23 
24 // --- MultiTouchMotionAccumulator ---
25 
MultiTouchMotionAccumulator()26 MultiTouchMotionAccumulator::MultiTouchMotionAccumulator()
27       : mCurrentSlot(-1), mUsingSlotsProtocol(false) {}
28 
configure(const InputDeviceContext & deviceContext,size_t slotCount,bool usingSlotsProtocol)29 void MultiTouchMotionAccumulator::configure(const InputDeviceContext& deviceContext,
30                                             size_t slotCount, bool usingSlotsProtocol) {
31     mUsingSlotsProtocol = usingSlotsProtocol;
32     mSlots = std::vector<Slot>(slotCount);
33 
34     mCurrentSlot = -1;
35     if (mUsingSlotsProtocol) {
36         // Query the driver for the current slot index and use it as the initial slot before we
37         // start reading events from the device.  It is possible that the current slot index will
38         // not be the same as it was when the first event was written into the evdev buffer, which
39         // means the input mapper could start out of sync with the initial state of the events in
40         // the evdev buffer. In the extremely unlikely case that this happens, the data from two
41         // slots will be confused until the next ABS_MT_SLOT event is received. This can cause the
42         // touch point to "jump", but at least there will be no stuck touches.
43         int32_t initialSlot;
44         if (const auto status = deviceContext.getAbsoluteAxisValue(ABS_MT_SLOT, &initialSlot);
45             status == OK) {
46             mCurrentSlot = initialSlot;
47         } else {
48             ALOGD("Could not retrieve current multi-touch slot index. status=%d", status);
49         }
50     }
51 }
52 
resetSlots()53 void MultiTouchMotionAccumulator::resetSlots() {
54     for (Slot& slot : mSlots) {
55         slot.clear();
56     }
57     mCurrentSlot = -1;
58 }
59 
process(const RawEvent * rawEvent)60 void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
61     if (rawEvent->type == EV_ABS) {
62         bool newSlot = false;
63         if (mUsingSlotsProtocol) {
64             if (rawEvent->code == ABS_MT_SLOT) {
65                 mCurrentSlot = rawEvent->value;
66                 newSlot = true;
67             }
68         } else if (mCurrentSlot < 0) {
69             mCurrentSlot = 0;
70         }
71 
72         if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlots.size()) {
73             if (newSlot) {
74                 ALOGW_IF(DEBUG_POINTERS,
75                          "MultiTouch device emitted invalid slot index %d but it "
76                          "should be between 0 and %zd; ignoring this slot.",
77                          mCurrentSlot, mSlots.size() - 1);
78             }
79         } else {
80             Slot& slot = mSlots[mCurrentSlot];
81             // If mUsingSlotsProtocol is true, it means the raw pointer has axis info of
82             // ABS_MT_TRACKING_ID and ABS_MT_SLOT, so driver should send a valid trackingId while
83             // updating the slot.
84             if (!mUsingSlotsProtocol) {
85                 slot.mInUse = true;
86             }
87 
88             switch (rawEvent->code) {
89                 case ABS_MT_POSITION_X:
90                     slot.mAbsMtPositionX = rawEvent->value;
91                     warnIfNotInUse(*rawEvent, slot);
92                     break;
93                 case ABS_MT_POSITION_Y:
94                     slot.mAbsMtPositionY = rawEvent->value;
95                     warnIfNotInUse(*rawEvent, slot);
96                     break;
97                 case ABS_MT_TOUCH_MAJOR:
98                     slot.mAbsMtTouchMajor = rawEvent->value;
99                     break;
100                 case ABS_MT_TOUCH_MINOR:
101                     slot.mAbsMtTouchMinor = rawEvent->value;
102                     slot.mHaveAbsMtTouchMinor = true;
103                     break;
104                 case ABS_MT_WIDTH_MAJOR:
105                     slot.mAbsMtWidthMajor = rawEvent->value;
106                     break;
107                 case ABS_MT_WIDTH_MINOR:
108                     slot.mAbsMtWidthMinor = rawEvent->value;
109                     slot.mHaveAbsMtWidthMinor = true;
110                     break;
111                 case ABS_MT_ORIENTATION:
112                     slot.mAbsMtOrientation = rawEvent->value;
113                     break;
114                 case ABS_MT_TRACKING_ID:
115                     if (mUsingSlotsProtocol && rawEvent->value < 0) {
116                         // The slot is no longer in use but it retains its previous contents,
117                         // which may be reused for subsequent touches.
118                         slot.mInUse = false;
119                     } else {
120                         slot.mInUse = true;
121                         slot.mAbsMtTrackingId = rawEvent->value;
122                     }
123                     break;
124                 case ABS_MT_PRESSURE:
125                     slot.mAbsMtPressure = rawEvent->value;
126                     break;
127                 case ABS_MT_DISTANCE:
128                     slot.mAbsMtDistance = rawEvent->value;
129                     break;
130                 case ABS_MT_TOOL_TYPE:
131                     slot.mAbsMtToolType = rawEvent->value;
132                     slot.mHaveAbsMtToolType = true;
133                     break;
134             }
135         }
136     } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
137         // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
138         mCurrentSlot += 1;
139     }
140 }
141 
finishSync()142 void MultiTouchMotionAccumulator::finishSync() {
143     if (!mUsingSlotsProtocol) {
144         resetSlots();
145     }
146 }
147 
warnIfNotInUse(const RawEvent & event,const Slot & slot)148 void MultiTouchMotionAccumulator::warnIfNotInUse(const RawEvent& event, const Slot& slot) {
149     if (!slot.mInUse) {
150         ALOGW("Received unexpected event (0x%0x, 0x%0x) for slot %i with tracking id %i",
151               event.code, event.value, mCurrentSlot, slot.mAbsMtTrackingId);
152     }
153 }
154 
155 // --- MultiTouchMotionAccumulator::Slot ---
156 
getToolType() const157 ToolType MultiTouchMotionAccumulator::Slot::getToolType() const {
158     if (mHaveAbsMtToolType) {
159         switch (mAbsMtToolType) {
160             case MT_TOOL_FINGER:
161                 return ToolType::FINGER;
162             case MT_TOOL_PEN:
163                 return ToolType::STYLUS;
164             case MT_TOOL_PALM:
165                 return ToolType::PALM;
166         }
167     }
168     return ToolType::UNKNOWN;
169 }
170 
171 } // namespace android
172