blob: d9b075f3ee7da444a33df618c15837f8acb3127f [file] [log] [blame]
Prabir Pradhanb56e92c2023-06-09 23:40:37 +00001/*
2 * Copyright 2023 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 "InputListener.h"
20#include "NotifyArgs.h"
21#include "PointerChoreographerPolicyInterface.h"
22
Byoungho Jungda10dd32023-10-06 17:03:45 +090023#include <android-base/thread_annotations.h>
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000024#include <gui/WindowInfosListener.h>
Prabir Pradhan19767602023-11-03 16:53:31 +000025#include <type_traits>
Arpit Singh420d0742024-04-04 11:54:20 +000026#include <unordered_set>
Byoungho Jungda10dd32023-10-06 17:03:45 +090027
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000028namespace android {
29
Byoungho Jung99326452023-11-03 20:19:17 +090030struct SpriteIcon;
31
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000032/**
Prabir Pradhan19767602023-11-03 16:53:31 +000033 * A helper class that wraps a factory method that acts as a constructor for the type returned
34 * by the factory method.
35 */
36template <typename Factory>
37struct ConstructorDelegate {
38 constexpr ConstructorDelegate(Factory&& factory) : mFactory(std::move(factory)) {}
39
40 using ConstructedType = std::invoke_result_t<const Factory&>;
41 constexpr operator ConstructedType() const { return mFactory(); }
42
43 Factory mFactory;
44};
45
46/**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000047 * PointerChoreographer manages the icons shown by the system for input interactions.
48 * This includes showing the mouse cursor, stylus hover icons, and touch spots.
49 * It is responsible for accumulating the location of the mouse cursor, and populating
50 * the cursor position for incoming events, if necessary.
51 */
52class PointerChoreographerInterface : public InputListenerInterface {
53public:
54 /**
Byoungho Jungda10dd32023-10-06 17:03:45 +090055 * Set the display that pointers, like the mouse cursor and drawing tablets,
56 * should be drawn on.
57 */
Linnan Li13bf76a2024-05-05 19:18:02 +080058 virtual void setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) = 0;
Byoungho Jungda10dd32023-10-06 17:03:45 +090059 virtual void setDisplayViewports(const std::vector<DisplayViewport>& viewports) = 0;
60 virtual std::optional<DisplayViewport> getViewportForPointerDevice(
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070061 ui::LogicalDisplayId associatedDisplayId = ui::LogicalDisplayId::INVALID) = 0;
Linnan Li13bf76a2024-05-05 19:18:02 +080062 virtual FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId) = 0;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090063 virtual void setShowTouchesEnabled(bool enabled) = 0;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090064 virtual void setStylusPointerIconEnabled(bool enabled) = 0;
Prabir Pradhan521f4fc2023-12-04 19:09:59 +000065 /**
66 * Set the icon that is shown for the given pointer. The request may fail in some cases, such
67 * as if the device or display was removed, or if the cursor was moved to a different display.
68 * Returns true if the icon was changed successfully, false otherwise.
69 */
Byoungho Jung99326452023-11-03 20:19:17 +090070 virtual bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
Linnan Li13bf76a2024-05-05 19:18:02 +080071 ui::LogicalDisplayId displayId, DeviceId deviceId) = 0;
Prabir Pradhan502ddbd2024-01-19 02:22:38 +000072 /**
73 * Set whether pointer icons for mice, touchpads, and styluses should be visible on the
74 * given display.
75 */
Linnan Li13bf76a2024-05-05 19:18:02 +080076 virtual void setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) = 0;
Byoungho Jung99326452023-11-03 20:19:17 +090077
Byoungho Jungda10dd32023-10-06 17:03:45 +090078 /**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000079 * This method may be called on any thread (usually by the input manager on a binder thread).
80 */
81 virtual void dump(std::string& dump) = 0;
82};
83
84class PointerChoreographer : public PointerChoreographerInterface {
85public:
86 explicit PointerChoreographer(InputListenerInterface& listener,
87 PointerChoreographerPolicyInterface&);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000088 ~PointerChoreographer() override;
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000089
Linnan Li13bf76a2024-05-05 19:18:02 +080090 void setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) override;
Byoungho Jungda10dd32023-10-06 17:03:45 +090091 void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override;
92 std::optional<DisplayViewport> getViewportForPointerDevice(
Linnan Li13bf76a2024-05-05 19:18:02 +080093 ui::LogicalDisplayId associatedDisplayId) override;
94 FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId) override;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090095 void setShowTouchesEnabled(bool enabled) override;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090096 void setStylusPointerIconEnabled(bool enabled) override;
Byoungho Jung99326452023-11-03 20:19:17 +090097 bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
Linnan Li13bf76a2024-05-05 19:18:02 +080098 ui::LogicalDisplayId displayId, DeviceId deviceId) override;
99 void setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) override;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900100
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000101 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
102 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
103 void notifyKey(const NotifyKeyArgs& args) override;
104 void notifyMotion(const NotifyMotionArgs& args) override;
105 void notifySwitch(const NotifySwitchArgs& args) override;
106 void notifySensor(const NotifySensorArgs& args) override;
107 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
108 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
109 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
110
111 void dump(std::string& dump) override;
112
113private:
Linnan Li13bf76a2024-05-05 19:18:02 +0800114 using PointerDisplayChange = std::optional<
115 std::tuple<ui::LogicalDisplayId /*displayId*/, FloatPoint /*cursorPosition*/>>;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000116 [[nodiscard]] PointerDisplayChange updatePointerControllersLocked() REQUIRES(mLock);
117 [[nodiscard]] PointerDisplayChange calculatePointerDisplayChangeToNotify() REQUIRES(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800118 const DisplayViewport* findViewportByIdLocked(ui::LogicalDisplayId displayId) const
119 REQUIRES(mLock);
120 ui::LogicalDisplayId getTargetMouseDisplayLocked(ui::LogicalDisplayId associatedDisplayId) const
121 REQUIRES(mLock);
122 std::pair<ui::LogicalDisplayId /*displayId*/, PointerControllerInterface&>
123 ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900124 InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800125 bool canUnfadeOnDisplay(ui::LogicalDisplayId displayId) REQUIRES(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900126
127 NotifyMotionArgs processMotion(const NotifyMotionArgs& args);
128 NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900129 NotifyMotionArgs processTouchpadEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000130 void processDrawingTabletEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900131 void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900132 void processStylusHoverEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900133 void processDeviceReset(const NotifyDeviceResetArgs& args);
Arpit Singh420d0742024-04-04 11:54:20 +0000134 void onControllerAddedOrRemovedLocked() REQUIRES(mLock);
135 void onPrivacySensitiveDisplaysChangedLocked(
136 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays)
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000137 REQUIRES(mLock);
Arpit Singhbd49b282024-05-23 18:02:54 +0000138 void onPrivacySensitiveDisplaysChanged(
139 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000140
Arpit Singh420d0742024-04-04 11:54:20 +0000141 /* This listener keeps tracks of visible privacy sensitive displays and updates the
142 * choreographer if there are any changes.
143 *
144 * Listener uses mListenerLock to guard all private data as choreographer and SurfaceComposer
145 * both can call into the listener. To prevent deadlocks Choreographer can call listener with
146 * its lock held, but listener must not call choreographer with its lock.
147 */
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000148 class PointerChoreographerDisplayInfoListener : public gui::WindowInfosListener {
149 public:
150 explicit PointerChoreographerDisplayInfoListener(PointerChoreographer* pc)
151 : mPointerChoreographer(pc){};
152 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
Arpit Singh420d0742024-04-04 11:54:20 +0000153 void setInitialDisplayInfos(const std::vector<gui::WindowInfo>& windowInfos);
154 std::unordered_set<ui::LogicalDisplayId /*displayId*/> getPrivacySensitiveDisplays();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000155 void onPointerChoreographerDestroyed();
156
157 private:
158 std::mutex mListenerLock;
159 PointerChoreographer* mPointerChoreographer GUARDED_BY(mListenerLock);
Arpit Singh420d0742024-04-04 11:54:20 +0000160 std::unordered_set<ui::LogicalDisplayId /*displayId*/> mPrivacySensitiveDisplays
161 GUARDED_BY(mListenerLock);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000162 };
163 sp<PointerChoreographerDisplayInfoListener> mWindowInfoListener GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900164
Prabir Pradhan19767602023-11-03 16:53:31 +0000165 using ControllerConstructor =
166 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
Prabir Pradhan16788792023-11-08 21:07:21 +0000167 ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800168 ControllerConstructor getMouseControllerConstructor(ui::LogicalDisplayId displayId)
169 REQUIRES(mLock);
170 ControllerConstructor getStylusControllerConstructor(ui::LogicalDisplayId displayId)
171 REQUIRES(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000172
Byoungho Jungda10dd32023-10-06 17:03:45 +0900173 std::mutex mLock;
174
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000175 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900176 PointerChoreographerPolicyInterface& mPolicy;
177
Linnan Li13bf76a2024-05-05 19:18:02 +0800178 std::map<ui::LogicalDisplayId, std::shared_ptr<PointerControllerInterface>>
179 mMousePointersByDisplay GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900180 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice
181 GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900182 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice
183 GUARDED_BY(mLock);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000184 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mDrawingTabletPointersByDevice
185 GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900186
Linnan Li13bf76a2024-05-05 19:18:02 +0800187 ui::LogicalDisplayId mDefaultMouseDisplayId GUARDED_BY(mLock);
188 ui::LogicalDisplayId mNotifiedPointerDisplayId GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900189 std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock);
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000190 std::set<DeviceId> mMouseDevices GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900191 std::vector<DisplayViewport> mViewports GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900192 bool mShowTouchesEnabled GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900193 bool mStylusPointerIconEnabled GUARDED_BY(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800194 std::set<ui::LogicalDisplayId /*displayId*/> mDisplaysWithPointersHidden;
Arpit Singhbd49b282024-05-23 18:02:54 +0000195
196protected:
197 using WindowListenerRegisterConsumer = std::function<std::vector<gui::WindowInfo>(
198 const sp<android::gui::WindowInfosListener>&)>;
199 using WindowListenerUnregisterConsumer =
200 std::function<void(const sp<android::gui::WindowInfosListener>&)>;
201 explicit PointerChoreographer(InputListenerInterface& listener,
202 PointerChoreographerPolicyInterface&,
203 const WindowListenerRegisterConsumer& registerListener,
204 const WindowListenerUnregisterConsumer& unregisterListener);
205
206private:
207 const WindowListenerRegisterConsumer mRegisterListener;
208 const WindowListenerUnregisterConsumer mUnregisterListener;
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000209};
210
211} // namespace android