• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             const sp<SpriteController>& spriteController);
51 
52     ~PointerController() override;
53 
54     std::optional<FloatRect> getBounds() const override;
55     void move(float deltaX, float deltaY) override;
56     void setPosition(float x, float y) override;
57     FloatPoint getPosition() const override;
58     int32_t getDisplayId() const override;
59     void fade(Transition transition) override;
60     void unfade(Transition transition) override;
61     void setDisplayViewport(const DisplayViewport& viewport) override;
62 
63     void setPresentation(Presentation presentation) override;
64     void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
65                   BitSet32 spotIdBits, int32_t displayId) override;
66     void clearSpots() override;
67 
68     void updatePointerIcon(PointerIconStyle iconId);
69     void setCustomPointerIcon(const SpriteIcon& icon);
70     void setInactivityTimeout(InactivityTimeout inactivityTimeout);
71     void doInactivityTimeout();
72     void reloadPointerResources();
73     void onDisplayViewportsUpdated(std::vector<DisplayViewport>& viewports);
74 
75     void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos)
76             REQUIRES(getLock());
77 
78     void dump(std::string& dump);
79 
80 protected:
81     using WindowListenerConsumer =
82             std::function<void(const sp<android::gui::WindowInfosListener>&)>;
83 
84     // Constructor used to test WindowInfosListener registration.
85     PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
86                       const sp<SpriteController>& spriteController,
87                       WindowListenerConsumer registerListener,
88                       WindowListenerConsumer unregisterListener);
89 
90 private:
91     PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
92                       const sp<SpriteController>& spriteController);
93 
94     friend PointerControllerContext::LooperCallback;
95     friend PointerControllerContext::MessageHandler;
96 
97     // PointerController's DisplayInfoListener can outlive the PointerController because when the
98     // listener is registered, a strong pointer to the listener (which can extend its lifecycle)
99     // is given away. To avoid the small overhead of using two separate locks in these two objects,
100     // we use the DisplayInfoListener's lock in PointerController.
101     std::mutex& getLock() const;
102 
103     PointerControllerContext mContext;
104 
105     MouseCursorController mCursorController;
106 
107     struct Locked {
108         Presentation presentation;
109         int32_t pointerDisplayId = ADISPLAY_ID_NONE;
110 
111         std::vector<gui::DisplayInfo> mDisplayInfos;
112         std::unordered_map<int32_t /* displayId */, TouchSpotController> spotControllers;
113     } mLocked GUARDED_BY(getLock());
114 
115     class DisplayInfoListener : public gui::WindowInfosListener {
116     public:
DisplayInfoListener(PointerController * pc)117         explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
118         void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
119         void onPointerControllerDestroyed();
120 
121         // This lock is also used by PointerController. See PointerController::getLock().
122         std::mutex mLock;
123 
124     private:
125         PointerController* mPointerController GUARDED_BY(mLock);
126     };
127 
128     sp<DisplayInfoListener> mDisplayInfoListener;
129     const WindowListenerConsumer mUnregisterWindowInfosListener;
130 
131     const ui::Transform& getTransformForDisplayLocked(int displayId) const REQUIRES(getLock());
132 
133     void clearSpotsLocked() REQUIRES(getLock());
134 };
135 
136 } // namespace android
137 
138 #endif // _UI_POINTER_CONTROLLER_H
139