xref: /aosp_15_r20/frameworks/base/libs/input/PointerController.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2010 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 #ifndef _UI_POINTER_CONTROLLER_H
18 #define _UI_POINTER_CONTROLLER_H
19 
20 #include <PointerControllerInterface.h>
21 #include <gui/DisplayEventReceiver.h>
22 #include <gui/WindowInfosUpdate.h>
23 #include <input/DisplayViewport.h>
24 #include <input/Input.h>
25 #include <utils/BitSet.h>
26 #include <utils/Looper.h>
27 #include <utils/RefBase.h>
28 
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 #include "MouseCursorController.h"
35 #include "PointerControllerContext.h"
36 #include "SpriteController.h"
37 #include "TouchSpotController.h"
38 
39 namespace android {
40 
41 /*
42  * Tracks pointer movements and draws the pointer sprite to a surface.
43  *
44  * Handles pointer acceleration and animation.
45  */
46 class PointerController : public PointerControllerInterface {
47 public:
48     static std::shared_ptr<PointerController> create(
49             const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
50             SpriteController& spriteController, ControllerType type);
51 
52     ~PointerController() override;
53 
54     vec2 move(float deltaX, float deltaY) override;
55     void setPosition(float x, float y) override;
56     vec2 getPosition() const override;
57     ui::LogicalDisplayId getDisplayId() const override;
58     void fade(Transition transition) override;
59     void unfade(Transition transition) override;
60     void setDisplayViewport(const DisplayViewport& viewport) override;
61 
62     void setPresentation(Presentation presentation) override;
63     void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
64                   BitSet32 spotIdBits, ui::LogicalDisplayId displayId) override;
65     void clearSpots() override;
66     void updatePointerIcon(PointerIconStyle iconId) override;
67     void setCustomPointerIcon(const SpriteIcon& icon) override;
68     void setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) override;
69     void clearSkipScreenshotFlags() override;
70     ui::Transform getDisplayTransform() const override;
71 
72     virtual void setInactivityTimeout(InactivityTimeout inactivityTimeout);
73     void doInactivityTimeout();
74     void reloadPointerResources();
75     void onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports);
76 
77     void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos)
78             REQUIRES(getLock());
79 
80     std::string dump() override;
81 
82 protected:
83     using WindowListenerRegisterConsumer = std::function<std::vector<gui::DisplayInfo>(
84             const sp<android::gui::WindowInfosListener>&)>;
85     using WindowListenerUnregisterConsumer =
86             std::function<void(const sp<android::gui::WindowInfosListener>&)>;
87 
88     // Constructor used to test WindowInfosListener registration.
89     PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
90                       SpriteController& spriteController,
91                       const WindowListenerRegisterConsumer& registerListener,
92                       WindowListenerUnregisterConsumer unregisterListener);
93 
94     PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
95                       SpriteController& spriteController);
96 
97 private:
98     friend PointerControllerContext::LooperCallback;
99     friend PointerControllerContext::MessageHandler;
100 
101     // PointerController's DisplayInfoListener can outlive the PointerController because when the
102     // listener is registered, a strong pointer to the listener (which can extend its lifecycle)
103     // is given away. To avoid the small overhead of using two separate locks in these two objects,
104     // we use the DisplayInfoListener's lock in PointerController.
105     std::mutex& getLock() const;
106 
107     PointerControllerContext mContext;
108 
109     MouseCursorController mCursorController;
110 
111     struct Locked {
112         Presentation presentation;
113         ui::LogicalDisplayId pointerDisplayId = ui::LogicalDisplayId::INVALID;
114 
115         std::vector<gui::DisplayInfo> mDisplayInfos;
116         std::unordered_map<ui::LogicalDisplayId, TouchSpotController> spotControllers;
117         std::unordered_set<ui::LogicalDisplayId> displaysToSkipScreenshot;
118     } mLocked GUARDED_BY(getLock());
119 
120     class DisplayInfoListener : public gui::WindowInfosListener {
121     public:
DisplayInfoListener(PointerController * pc)122         explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
123         void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
124         void onPointerControllerDestroyed();
125 
126         // This lock is also used by PointerController. See PointerController::getLock().
127         std::mutex mLock;
128 
129     private:
130         PointerController* mPointerController GUARDED_BY(mLock);
131     };
132 
133     sp<DisplayInfoListener> mDisplayInfoListener;
134     const WindowListenerUnregisterConsumer mUnregisterWindowInfosListener;
135 
136     const ui::Transform& getTransformForDisplayLocked(ui::LogicalDisplayId displayId) const
137             REQUIRES(getLock());
138 
139     void clearSpotsLocked() REQUIRES(getLock());
140 };
141 
142 class MousePointerController : public PointerController {
143 public:
144     /** A version of PointerController that controls one mouse pointer. */
145     MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
146                            const sp<Looper>& looper, SpriteController& spriteController);
147 
148     ~MousePointerController() override;
149 
setPresentation(Presentation)150     void setPresentation(Presentation) override {
151         LOG_ALWAYS_FATAL("Should not be called");
152     }
setSpots(const PointerCoords *,const uint32_t *,BitSet32,ui::LogicalDisplayId)153     void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override {
154         LOG_ALWAYS_FATAL("Should not be called");
155     }
clearSpots()156     void clearSpots() override {
157         LOG_ALWAYS_FATAL("Should not be called");
158     }
159 };
160 
161 class TouchPointerController : public PointerController {
162 public:
163     /** A version of PointerController that controls touch spots. */
164     TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
165                            const sp<Looper>& looper, SpriteController& spriteController);
166 
167     ~TouchPointerController() override;
168 
move(float,float)169     vec2 move(float, float) override {
170         LOG_ALWAYS_FATAL("Should not be called");
171     }
setPosition(float,float)172     void setPosition(float, float) override {
173         LOG_ALWAYS_FATAL("Should not be called");
174     }
getPosition()175     vec2 getPosition() const override {
176         LOG_ALWAYS_FATAL("Should not be called");
177     }
getDisplayId()178     ui::LogicalDisplayId getDisplayId() const override {
179         LOG_ALWAYS_FATAL("Should not be called");
180     }
fade(Transition)181     void fade(Transition) override {
182         LOG_ALWAYS_FATAL("Should not be called");
183     }
unfade(Transition)184     void unfade(Transition) override {
185         LOG_ALWAYS_FATAL("Should not be called");
186     }
setDisplayViewport(const DisplayViewport &)187     void setDisplayViewport(const DisplayViewport&) override {
188         LOG_ALWAYS_FATAL("Should not be called");
189     }
setPresentation(Presentation)190     void setPresentation(Presentation) override {
191         LOG_ALWAYS_FATAL("Should not be called");
192     }
updatePointerIcon(PointerIconStyle)193     void updatePointerIcon(PointerIconStyle) override {
194         LOG_ALWAYS_FATAL("Should not be called");
195     }
setCustomPointerIcon(const SpriteIcon &)196     void setCustomPointerIcon(const SpriteIcon&) override {
197         LOG_ALWAYS_FATAL("Should not be called");
198     }
199     // fade() should not be called by inactivity timeout. Do nothing.
setInactivityTimeout(InactivityTimeout)200     void setInactivityTimeout(InactivityTimeout) override {}
201 };
202 
203 class StylusPointerController : public PointerController {
204 public:
205     /** A version of PointerController that controls one stylus pointer. */
206     StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
207                             const sp<Looper>& looper, SpriteController& spriteController);
208 
209     ~StylusPointerController() override;
210 
setPresentation(Presentation)211     void setPresentation(Presentation) override {
212         LOG_ALWAYS_FATAL("Should not be called");
213     }
setSpots(const PointerCoords *,const uint32_t *,BitSet32,ui::LogicalDisplayId)214     void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override {
215         LOG_ALWAYS_FATAL("Should not be called");
216     }
clearSpots()217     void clearSpots() override {
218         LOG_ALWAYS_FATAL("Should not be called");
219     }
220 };
221 
222 } // namespace android
223 
224 #endif // _UI_POINTER_CONTROLLER_H
225