blob: b29d9cdb7c126339a5e14f278e00150846353760 [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>
Byoungho Jungda10dd32023-10-06 17:03:45 +090026
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000027namespace android {
28
Byoungho Jung99326452023-11-03 20:19:17 +090029struct SpriteIcon;
30
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000031/**
Prabir Pradhan19767602023-11-03 16:53:31 +000032 * A helper class that wraps a factory method that acts as a constructor for the type returned
33 * by the factory method.
34 */
35template <typename Factory>
36struct ConstructorDelegate {
37 constexpr ConstructorDelegate(Factory&& factory) : mFactory(std::move(factory)) {}
38
39 using ConstructedType = std::invoke_result_t<const Factory&>;
40 constexpr operator ConstructedType() const { return mFactory(); }
41
42 Factory mFactory;
43};
44
45/**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000046 * PointerChoreographer manages the icons shown by the system for input interactions.
47 * This includes showing the mouse cursor, stylus hover icons, and touch spots.
48 * It is responsible for accumulating the location of the mouse cursor, and populating
49 * the cursor position for incoming events, if necessary.
50 */
51class PointerChoreographerInterface : public InputListenerInterface {
52public:
53 /**
Byoungho Jungda10dd32023-10-06 17:03:45 +090054 * Set the display that pointers, like the mouse cursor and drawing tablets,
55 * should be drawn on.
56 */
57 virtual void setDefaultMouseDisplayId(int32_t displayId) = 0;
58 virtual void setDisplayViewports(const std::vector<DisplayViewport>& viewports) = 0;
59 virtual std::optional<DisplayViewport> getViewportForPointerDevice(
60 int32_t associatedDisplayId = ADISPLAY_ID_NONE) = 0;
61 virtual FloatPoint getMouseCursorPosition(int32_t displayId) = 0;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090062 virtual void setShowTouchesEnabled(bool enabled) = 0;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090063 virtual void setStylusPointerIconEnabled(bool enabled) = 0;
Prabir Pradhan521f4fc2023-12-04 19:09:59 +000064 /**
65 * Set the icon that is shown for the given pointer. The request may fail in some cases, such
66 * as if the device or display was removed, or if the cursor was moved to a different display.
67 * Returns true if the icon was changed successfully, false otherwise.
68 */
Byoungho Jung99326452023-11-03 20:19:17 +090069 virtual bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
70 int32_t displayId, DeviceId deviceId) = 0;
Prabir Pradhan502ddbd2024-01-19 02:22:38 +000071 /**
72 * Set whether pointer icons for mice, touchpads, and styluses should be visible on the
73 * given display.
74 */
75 virtual void setPointerIconVisibility(int32_t displayId, bool visible) = 0;
Byoungho Jung99326452023-11-03 20:19:17 +090076
Byoungho Jungda10dd32023-10-06 17:03:45 +090077 /**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000078 * This method may be called on any thread (usually by the input manager on a binder thread).
79 */
80 virtual void dump(std::string& dump) = 0;
81};
82
83class PointerChoreographer : public PointerChoreographerInterface {
84public:
85 explicit PointerChoreographer(InputListenerInterface& listener,
86 PointerChoreographerPolicyInterface&);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000087 ~PointerChoreographer() override;
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000088
Byoungho Jungda10dd32023-10-06 17:03:45 +090089 void setDefaultMouseDisplayId(int32_t displayId) override;
90 void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override;
91 std::optional<DisplayViewport> getViewportForPointerDevice(
92 int32_t associatedDisplayId) override;
93 FloatPoint getMouseCursorPosition(int32_t displayId) override;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090094 void setShowTouchesEnabled(bool enabled) override;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090095 void setStylusPointerIconEnabled(bool enabled) override;
Byoungho Jung99326452023-11-03 20:19:17 +090096 bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
97 int32_t displayId, DeviceId deviceId) override;
Prabir Pradhan502ddbd2024-01-19 02:22:38 +000098 void setPointerIconVisibility(int32_t displayId, bool visible) override;
Byoungho Jungda10dd32023-10-06 17:03:45 +090099
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000100 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
101 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
102 void notifyKey(const NotifyKeyArgs& args) override;
103 void notifyMotion(const NotifyMotionArgs& args) override;
104 void notifySwitch(const NotifySwitchArgs& args) override;
105 void notifySensor(const NotifySensorArgs& args) override;
106 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
107 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
108 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
109
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000110 // Public because it's also used by tests to simulate the WindowInfosListener callback
111 void onWindowInfosChanged(const std::vector<android::gui::WindowInfo>& windowInfos);
112
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000113 void dump(std::string& dump) override;
114
115private:
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000116 using PointerDisplayChange =
117 std::optional<std::tuple<int32_t /*displayId*/, FloatPoint /*cursorPosition*/>>;
118 [[nodiscard]] PointerDisplayChange updatePointerControllersLocked() REQUIRES(mLock);
119 [[nodiscard]] PointerDisplayChange calculatePointerDisplayChangeToNotify() REQUIRES(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900120 const DisplayViewport* findViewportByIdLocked(int32_t displayId) const REQUIRES(mLock);
121 int32_t getTargetMouseDisplayLocked(int32_t associatedDisplayId) const REQUIRES(mLock);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000122 std::pair<int32_t /*displayId*/, PointerControllerInterface&> ensureMouseControllerLocked(
Byoungho Jungee6268f2023-10-30 17:27:26 +0900123 int32_t associatedDisplayId) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900124 InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000125 bool canUnfadeOnDisplay(int32_t 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 Singh4b6ad2d2024-04-04 11:54:20 +0000134 void onControllerAddedOrRemoved() REQUIRES(mLock);
135 void onWindowInfosChangedLocked(const std::vector<android::gui::WindowInfo>& windowInfos)
136 REQUIRES(mLock);
137
138 class PointerChoreographerDisplayInfoListener : public gui::WindowInfosListener {
139 public:
140 explicit PointerChoreographerDisplayInfoListener(PointerChoreographer* pc)
141 : mPointerChoreographer(pc){};
142 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
143 void onPointerChoreographerDestroyed();
144
145 private:
146 std::mutex mListenerLock;
147 PointerChoreographer* mPointerChoreographer GUARDED_BY(mListenerLock);
148 };
149 sp<PointerChoreographerDisplayInfoListener> mWindowInfoListener GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900150
Prabir Pradhan19767602023-11-03 16:53:31 +0000151 using ControllerConstructor =
152 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
Prabir Pradhan16788792023-11-08 21:07:21 +0000153 ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000154 ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900155 ControllerConstructor getStylusControllerConstructor(int32_t displayId) REQUIRES(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000156
Byoungho Jungda10dd32023-10-06 17:03:45 +0900157 std::mutex mLock;
158
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000159 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900160 PointerChoreographerPolicyInterface& mPolicy;
161
162 std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay
163 GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900164 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice
165 GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900166 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice
167 GUARDED_BY(mLock);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000168 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mDrawingTabletPointersByDevice
169 GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900170
171 int32_t mDefaultMouseDisplayId GUARDED_BY(mLock);
172 int32_t mNotifiedPointerDisplayId GUARDED_BY(mLock);
173 std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock);
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000174 std::set<DeviceId> mMouseDevices GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900175 std::vector<DisplayViewport> mViewports GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900176 bool mShowTouchesEnabled GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900177 bool mStylusPointerIconEnabled GUARDED_BY(mLock);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000178 std::set<int32_t /*displayId*/> mDisplaysWithPointersHidden;
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000179};
180
181} // namespace android