blob: 14068fd878865cb3a242fbeb33df89429491ec12 [file] [log] [blame]
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -07001/*
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#pragma once
18
19#include <map>
20#include <set>
21
22#include "include/UnwantedInteractionBlockerInterface.h"
23#include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h"
24#include "ui/events/ozone/evdev/touch_filter/palm_detection_filter.h"
25
26namespace android {
27
28// --- Functions for manipulation of event streams
29
30struct AndroidPalmFilterDeviceInfo : ::ui::PalmFilterDeviceInfo {
31 // Additional fields from 'TouchEventConverterEvdev', added here for convenience
32 int32_t touch_major_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MAJOR).resolution;
33 int32_t touch_minor_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MINOR).resolution;
34
35 auto operator<=>(const AndroidPalmFilterDeviceInfo&) const = default;
36};
37
38std::optional<AndroidPalmFilterDeviceInfo> createPalmFilterDeviceInfo(
39 const InputDeviceInfo& deviceInfo);
40
41static constexpr int32_t ACTION_UNKNOWN = -1;
42
43NotifyMotionArgs removePointerIds(const NotifyMotionArgs& args,
44 const std::set<int32_t>& pointerIds);
45
46std::vector<NotifyMotionArgs> cancelSuppressedPointers(
47 const NotifyMotionArgs& args, const std::set<int32_t>& oldSuppressedPointerIds,
48 const std::set<int32_t>& newSuppressedPointerIds);
49
50std::string toString(const ::ui::InProgressTouchEvdev& touch);
51
52// --- Main classes and interfaces ---
53
54class PalmRejector;
55
56// --- Implementations ---
57
58/**
59 * Implementation of the UnwantedInteractionBlockerInterface.
60 * Represents a separate stage of input processing. All of the input events go through this stage.
61 * Acts as a passthrough for all input events except for motion events.
62 *
63 * The events of motion type are sent to PalmRejectors. PalmRejectors detect unwanted touches,
64 * and emit input streams with the bad pointers removed.
65 */
66class UnwantedInteractionBlocker : public UnwantedInteractionBlockerInterface {
67public:
68 explicit UnwantedInteractionBlocker(InputListenerInterface& listener);
69 explicit UnwantedInteractionBlocker(InputListenerInterface& listener, bool enablePalmRejection);
70
71 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
72 void notifyKey(const NotifyKeyArgs* args) override;
73 void notifyMotion(const NotifyMotionArgs* args) override;
74 void notifySwitch(const NotifySwitchArgs* args) override;
75 void notifySensor(const NotifySensorArgs* args) override;
76 void notifyVibratorState(const NotifyVibratorStateArgs* args) override;
77 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
78 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override;
79
80 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override;
81 void dump(std::string& dump) override;
82 void monitor() override;
83
84 ~UnwantedInteractionBlocker();
85
86private:
87 // The next stage to pass input events to
88 InputListenerInterface& mListener;
89 const bool mEnablePalmRejection;
90
91 // Detect and reject unwanted palms on screen
92 // Use a separate palm rejector for every touch device.
93 std::map<int32_t /*deviceId*/, PalmRejector> mPalmRejectors;
94};
95
96class SlotState {
97public:
98 /**
99 * Update the state using the new information provided in the NotifyMotionArgs
100 */
101 void update(const NotifyMotionArgs& args);
102 std::optional<size_t> getSlotForPointerId(int32_t pointerId) const;
103 std::string dump() const;
104
105private:
106 // Process a pointer with the provided action, and return the slot associated with it
107 void processPointerId(int32_t pointerId, int32_t action);
108 // The map from tracking id to slot state. Since the PalmRejectionFilter works close to the
109 // evdev level, the only way to tell it about UP or CANCEL events is by sending tracking id = -1
110 // to the appropriate touch slot. So we need to reconstruct the original slot.
111 // The two collections below must always be in-sync.
112 // Use std::map instead of std::unordered_map because we rely on these collections being
113 // ordered. It also has better space efficiency than unordered_map because we only have a few
114 // pointers most of the time.
115 std::map<int32_t /*pointerId*/, size_t /*slot*/> mSlotsByPointerId;
116 std::map<size_t /*slot*/, int32_t /*pointerId */> mPointerIdsBySlot;
117
118 size_t findUnusedSlot() const;
119};
120
121/**
122 * Convert an Android event to a linux-like 'InProgressTouchEvdev'. The provided SlotState's
123 * are used to figure out which slot does each pointer belong to.
124 */
125std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args,
126 const AndroidPalmFilterDeviceInfo& deviceInfo,
127 const SlotState& oldSlotState,
128 const SlotState& newSlotState);
129
130class PalmRejector {
131public:
132 explicit PalmRejector(const AndroidPalmFilterDeviceInfo& info,
133 std::unique_ptr<::ui::PalmDetectionFilter> filter = nullptr);
134 std::vector<NotifyMotionArgs> processMotion(const NotifyMotionArgs& args);
135
136 // Get the device info of this device, for comparison purposes
137 const AndroidPalmFilterDeviceInfo& getPalmFilterDeviceInfo();
138 std::string dump() const;
139
140private:
141 PalmRejector(const PalmRejector&) = delete;
142 PalmRejector& operator=(const PalmRejector&) = delete;
143
144 std::unique_ptr<::ui::SharedPalmDetectionFilterState> mSharedPalmState;
145 AndroidPalmFilterDeviceInfo mDeviceInfo;
146 std::unique_ptr<::ui::PalmDetectionFilter> mPalmDetectionFilter;
147 std::set<int32_t> mSuppressedPointerIds;
148
149 // Used to help convert an Android touch stream to Linux input stream.
150 SlotState mSlotState;
151};
152
153} // namespace android