xref: /aosp_15_r20/frameworks/native/services/inputflinger/reader/mapper/TouchpadInputMapper.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 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 #pragma once
18 
19 #include <list>
20 #include <memory>
21 #include <set>
22 #include <vector>
23 
24 #include <PointerControllerInterface.h>
25 #include <utils/Timers.h>
26 
27 #include "CapturedTouchpadEventConverter.h"
28 #include "EventHub.h"
29 #include "InputDevice.h"
30 #include "InputMapper.h"
31 #include "InputReaderBase.h"
32 #include "NotifyArgs.h"
33 #include "accumulator/MultiTouchMotionAccumulator.h"
34 #include "gestures/GestureConverter.h"
35 #include "gestures/HardwareStateConverter.h"
36 #include "gestures/PropertyProvider.h"
37 #include "gestures/TimerProvider.h"
38 
39 #include "include/gestures.h"
40 
41 namespace android {
42 
43 class TouchpadInputMapper : public InputMapper {
44 public:
45     template <class T, class... Args>
46     friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
47                                                 const InputReaderConfiguration& readerConfig,
48                                                 Args... args);
49     ~TouchpadInputMapper();
50 
51     uint32_t getSources() const override;
52     void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
53     void dump(std::string& dump) override;
54 
55     [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
56                                                     const InputReaderConfiguration& config,
57                                                     ConfigurationChanges changes) override;
58     [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
59     [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override;
60     [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when) override;
61 
62     void consumeGesture(const Gesture* gesture);
63 
64     // A subset of InputDeviceIdentifier used for logging metrics, to avoid storing a copy of the
65     // strings in that bigger struct.
66     using MetricsIdentifier = std::tuple<uint16_t /*busId*/, uint16_t /*vendorId*/,
67                                          uint16_t /*productId*/, uint16_t /*version*/>;
68 
69     std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override;
70 
71     std::optional<HardwareProperties> getTouchpadHardwareProperties() override;
72 
73 private:
74     void resetGestureInterpreter(nsecs_t when);
75     explicit TouchpadInputMapper(InputDeviceContext& deviceContext,
76                                  const InputReaderConfiguration& readerConfig);
77     void updatePalmDetectionMetrics();
78     [[nodiscard]] std::list<NotifyArgs> sendHardwareState(nsecs_t when, nsecs_t readTime,
79                                                           SelfContainedHardwareState schs);
80     [[nodiscard]] std::list<NotifyArgs> processGestures(nsecs_t when, nsecs_t readTime);
81 
82     std::unique_ptr<gestures::GestureInterpreter, void (*)(gestures::GestureInterpreter*)>
83             mGestureInterpreter;
84 
85     PropertyProvider mPropertyProvider;
86     TimerProvider mTimerProvider;
87 
88     // The MultiTouchMotionAccumulator is shared between the HardwareStateConverter and
89     // CapturedTouchpadEventConverter, so that if the touchpad is captured or released while touches
90     // are down, the relevant converter can still benefit from the current axis values stored in the
91     // accumulator.
92     MultiTouchMotionAccumulator mMotionAccumulator;
93 
94     HardwareStateConverter mStateConverter;
95     GestureConverter mGestureConverter;
96     CapturedTouchpadEventConverter mCapturedEventConverter;
97     HardwareProperties mHardwareProperties;
98 
99     bool mPointerCaptured = false;
100     bool mResettingInterpreter = false;
101     std::vector<Gesture> mGesturesToProcess;
102 
metricsIdFromInputDeviceIdentifier(const InputDeviceIdentifier & id)103     static MetricsIdentifier metricsIdFromInputDeviceIdentifier(const InputDeviceIdentifier& id) {
104         return std::make_tuple(id.bus, id.vendor, id.product, id.version);
105     }
106     const MetricsIdentifier mMetricsId;
107     // Tracking IDs for touches on the pad in the last evdev frame.
108     std::set<int32_t> mLastFrameTrackingIds;
109     // Tracking IDs for touches that have at some point been reported as palms by the touchpad.
110     std::set<int32_t> mPalmTrackingIds;
111 
112     // The display that events generated by this mapper should target. This can be set to
113     // LogicalDisplayId::INVALID to target the focused display. If there is no display target (i.e.
114     // std::nullopt), all events will be ignored.
115     std::optional<ui::LogicalDisplayId> mDisplayId;
116 
117     nsecs_t mGestureStartTime{0};
118 
119     // True if hardware state update notifications is available for usage based on its feature flag
120     // and settings value.
121     bool mTouchpadHardwareStateNotificationsEnabled = false;
122 };
123 
124 } // namespace android
125