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