blob: 8ff965851fae133171c7652b8ba25480965ed3ea [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
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -070022#include <android-base/thread_annotations.h>
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070023#include "include/UnwantedInteractionBlockerInterface.h"
24#include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h"
25#include "ui/events/ozone/evdev/touch_filter/palm_detection_filter.h"
26
Siarhei Vishniakou814ace32022-03-04 15:12:16 -080027#include "PreferStylusOverTouchBlocker.h"
28
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070029namespace android {
30
31// --- Functions for manipulation of event streams
32
33struct AndroidPalmFilterDeviceInfo : ::ui::PalmFilterDeviceInfo {
34 // Additional fields from 'TouchEventConverterEvdev', added here for convenience
35 int32_t touch_major_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MAJOR).resolution;
36 int32_t touch_minor_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MINOR).resolution;
37
38 auto operator<=>(const AndroidPalmFilterDeviceInfo&) const = default;
39};
40
41std::optional<AndroidPalmFilterDeviceInfo> createPalmFilterDeviceInfo(
42 const InputDeviceInfo& deviceInfo);
43
44static constexpr int32_t ACTION_UNKNOWN = -1;
45
46NotifyMotionArgs removePointerIds(const NotifyMotionArgs& args,
47 const std::set<int32_t>& pointerIds);
48
49std::vector<NotifyMotionArgs> cancelSuppressedPointers(
50 const NotifyMotionArgs& args, const std::set<int32_t>& oldSuppressedPointerIds,
51 const std::set<int32_t>& newSuppressedPointerIds);
52
53std::string toString(const ::ui::InProgressTouchEvdev& touch);
54
55// --- Main classes and interfaces ---
56
57class PalmRejector;
58
59// --- Implementations ---
60
61/**
62 * Implementation of the UnwantedInteractionBlockerInterface.
63 * Represents a separate stage of input processing. All of the input events go through this stage.
64 * Acts as a passthrough for all input events except for motion events.
65 *
66 * The events of motion type are sent to PalmRejectors. PalmRejectors detect unwanted touches,
67 * and emit input streams with the bad pointers removed.
68 */
69class UnwantedInteractionBlocker : public UnwantedInteractionBlockerInterface {
70public:
71 explicit UnwantedInteractionBlocker(InputListenerInterface& listener);
72 explicit UnwantedInteractionBlocker(InputListenerInterface& listener, bool enablePalmRejection);
73
74 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
75 void notifyKey(const NotifyKeyArgs* args) override;
76 void notifyMotion(const NotifyMotionArgs* args) override;
77 void notifySwitch(const NotifySwitchArgs* args) override;
78 void notifySensor(const NotifySensorArgs* args) override;
79 void notifyVibratorState(const NotifyVibratorStateArgs* args) override;
80 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
81 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override;
82
83 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override;
84 void dump(std::string& dump) override;
85 void monitor() override;
86
87 ~UnwantedInteractionBlocker();
88
89private:
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -070090 std::mutex mLock;
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070091 // The next stage to pass input events to
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -070092
93 QueuedInputListener mQueuedListener;
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070094 const bool mEnablePalmRejection;
95
Siarhei Vishniakou814ace32022-03-04 15:12:16 -080096 // When stylus is down, ignore touch
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -070097 PreferStylusOverTouchBlocker mPreferStylusOverTouchBlocker GUARDED_BY(mLock);
Siarhei Vishniakou814ace32022-03-04 15:12:16 -080098
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070099 // Detect and reject unwanted palms on screen
100 // Use a separate palm rejector for every touch device.
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -0700101 std::map<int32_t /*deviceId*/, PalmRejector> mPalmRejectors GUARDED_BY(mLock);
Siarhei Vishniakou814ace32022-03-04 15:12:16 -0800102 // TODO(b/210159205): delete this when simultaneous stylus and touch is supported
Siarhei Vishniakoua91d8572022-05-17 05:03:42 -0700103 void notifyMotionLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +0000104
105 // Call this function for outbound events so that they can be logged when logging is enabled.
106 void enqueueOutboundMotionLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700107};
108
109class SlotState {
110public:
111 /**
112 * Update the state using the new information provided in the NotifyMotionArgs
113 */
114 void update(const NotifyMotionArgs& args);
115 std::optional<size_t> getSlotForPointerId(int32_t pointerId) const;
116 std::string dump() const;
117
118private:
119 // Process a pointer with the provided action, and return the slot associated with it
120 void processPointerId(int32_t pointerId, int32_t action);
121 // The map from tracking id to slot state. Since the PalmRejectionFilter works close to the
122 // evdev level, the only way to tell it about UP or CANCEL events is by sending tracking id = -1
123 // to the appropriate touch slot. So we need to reconstruct the original slot.
124 // The two collections below must always be in-sync.
125 // Use std::map instead of std::unordered_map because we rely on these collections being
126 // ordered. It also has better space efficiency than unordered_map because we only have a few
127 // pointers most of the time.
128 std::map<int32_t /*pointerId*/, size_t /*slot*/> mSlotsByPointerId;
129 std::map<size_t /*slot*/, int32_t /*pointerId */> mPointerIdsBySlot;
130
131 size_t findUnusedSlot() const;
132};
133
134/**
135 * Convert an Android event to a linux-like 'InProgressTouchEvdev'. The provided SlotState's
136 * are used to figure out which slot does each pointer belong to.
137 */
138std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args,
139 const AndroidPalmFilterDeviceInfo& deviceInfo,
140 const SlotState& oldSlotState,
141 const SlotState& newSlotState);
142
143class PalmRejector {
144public:
145 explicit PalmRejector(const AndroidPalmFilterDeviceInfo& info,
146 std::unique_ptr<::ui::PalmDetectionFilter> filter = nullptr);
147 std::vector<NotifyMotionArgs> processMotion(const NotifyMotionArgs& args);
148
149 // Get the device info of this device, for comparison purposes
150 const AndroidPalmFilterDeviceInfo& getPalmFilterDeviceInfo();
151 std::string dump() const;
152
153private:
154 PalmRejector(const PalmRejector&) = delete;
155 PalmRejector& operator=(const PalmRejector&) = delete;
156
157 std::unique_ptr<::ui::SharedPalmDetectionFilterState> mSharedPalmState;
158 AndroidPalmFilterDeviceInfo mDeviceInfo;
159 std::unique_ptr<::ui::PalmDetectionFilter> mPalmDetectionFilter;
160 std::set<int32_t> mSuppressedPointerIds;
161
162 // Used to help convert an Android touch stream to Linux input stream.
163 SlotState mSlotState;
164};
165
166} // namespace android