• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() const override;
70 
71     std::optional<HardwareProperties> getTouchpadHardwareProperties() override;
72 
73     std::optional<GesturesProp> getGesturePropertyForTesting(const std::string& name);
74 
75 private:
76     void resetGestureInterpreter(nsecs_t when);
77     explicit TouchpadInputMapper(InputDeviceContext& deviceContext,
78                                  const InputReaderConfiguration& readerConfig);
79     void updatePalmDetectionMetrics();
80     [[nodiscard]] std::list<NotifyArgs> sendHardwareState(nsecs_t when, nsecs_t readTime,
81                                                           SelfContainedHardwareState schs);
82     [[nodiscard]] std::list<NotifyArgs> processGestures(nsecs_t when, nsecs_t readTime);
83 
84     std::unique_ptr<gestures::GestureInterpreter, void (*)(gestures::GestureInterpreter*)>
85             mGestureInterpreter;
86 
87     PropertyProvider mPropertyProvider;
88     TimerProvider mTimerProvider;
89 
90     // The MultiTouchMotionAccumulator is shared between the HardwareStateConverter and
91     // CapturedTouchpadEventConverter, so that if the touchpad is captured or released while touches
92     // are down, the relevant converter can still benefit from the current axis values stored in the
93     // accumulator.
94     MultiTouchMotionAccumulator mMotionAccumulator;
95 
96     HardwareStateConverter mStateConverter;
97     GestureConverter mGestureConverter;
98     CapturedTouchpadEventConverter mCapturedEventConverter;
99     HardwareProperties mHardwareProperties;
100 
101     bool mPointerCaptured = false;
102     bool mResettingInterpreter = false;
103     std::vector<Gesture> mGesturesToProcess;
104 
metricsIdFromInputDeviceIdentifier(const InputDeviceIdentifier & id)105     static MetricsIdentifier metricsIdFromInputDeviceIdentifier(const InputDeviceIdentifier& id) {
106         return std::make_tuple(id.bus, id.vendor, id.product, id.version);
107     }
108     const MetricsIdentifier mMetricsId;
109     // Tracking IDs for touches on the pad in the last evdev frame.
110     std::set<int32_t> mLastFrameTrackingIds;
111     // Tracking IDs for touches that have at some point been reported as palms by the touchpad.
112     std::set<int32_t> mPalmTrackingIds;
113 
114     // The display that events generated by this mapper should target. This can be set to
115     // LogicalDisplayId::INVALID to target the focused display. If there is no display target (i.e.
116     // std::nullopt), all events will be ignored.
117     std::optional<ui::LogicalDisplayId> mDisplayId;
118 
119     nsecs_t mGestureStartTime{0};
120 
121     // True if hardware state update notifications is available for usage based on its feature flag
122     // and settings value.
123     bool mTouchpadHardwareStateNotificationsEnabled = false;
124 };
125 
126 } // namespace android
127