• 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 <input/DisplayViewport.h>
23 #include <input/Input.h>
24 #include <utils/BitSet.h>
25 #include <utils/Looper.h>
26 #include <utils/RefBase.h>
27 
28 #include <map>
29 #include <memory>
30 #include <vector>
31 
32 #include "MouseCursorController.h"
33 #include "PointerControllerContext.h"
34 #include "SpriteController.h"
35 #include "TouchSpotController.h"
36 
37 namespace android {
38 
39 /*
40  * Tracks pointer movements and draws the pointer sprite to a surface.
41  *
42  * Handles pointer acceleration and animation.
43  */
44 class PointerController : public PointerControllerInterface {
45 public:
46     static std::shared_ptr<PointerController> create(
47             const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
48             const sp<SpriteController>& spriteController);
49 
50     ~PointerController() override;
51 
52     virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
53     virtual void move(float deltaX, float deltaY);
54     virtual void setButtonState(int32_t buttonState);
55     virtual int32_t getButtonState() const;
56     virtual void setPosition(float x, float y);
57     virtual void getPosition(float* outX, float* outY) const;
58     virtual int32_t getDisplayId() const;
59     virtual void fade(Transition transition);
60     virtual void unfade(Transition transition);
61     virtual void setDisplayViewport(const DisplayViewport& viewport);
62 
63     virtual void setPresentation(Presentation presentation);
64     virtual void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
65                           BitSet32 spotIdBits, int32_t displayId);
66     virtual void clearSpots();
67 
68     void updatePointerIcon(int32_t 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 protected:
79     using WindowListenerConsumer =
80             std::function<void(const sp<android::gui::WindowInfosListener>&)>;
81 
82     // Constructor used to test WindowInfosListener registration.
83     PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
84                       const sp<SpriteController>& spriteController,
85                       WindowListenerConsumer registerListener,
86                       WindowListenerConsumer unregisterListener);
87 
88 private:
89     PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
90                       const sp<SpriteController>& spriteController);
91 
92     friend PointerControllerContext::LooperCallback;
93     friend PointerControllerContext::MessageHandler;
94 
95     // PointerController's DisplayInfoListener can outlive the PointerController because when the
96     // listener is registered, a strong pointer to the listener (which can extend its lifecycle)
97     // is given away. To avoid the small overhead of using two separate locks in these two objects,
98     // we use the DisplayInfoListener's lock in PointerController.
99     std::mutex& getLock() const;
100 
101     PointerControllerContext mContext;
102 
103     MouseCursorController mCursorController;
104 
105     struct Locked {
106         Presentation presentation;
107         int32_t pointerDisplayId = ADISPLAY_ID_NONE;
108 
109         std::vector<gui::DisplayInfo> mDisplayInfos;
110         std::unordered_map<int32_t /* displayId */, TouchSpotController> spotControllers;
111     } mLocked GUARDED_BY(getLock());
112 
113     class DisplayInfoListener : public gui::WindowInfosListener {
114     public:
DisplayInfoListener(PointerController * pc)115         explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
116         void onWindowInfosChanged(const std::vector<android::gui::WindowInfo>&,
117                                   const std::vector<android::gui::DisplayInfo>&) override;
118         void onPointerControllerDestroyed();
119 
120         // This lock is also used by PointerController. See PointerController::getLock().
121         std::mutex mLock;
122 
123     private:
124         PointerController* mPointerController GUARDED_BY(mLock);
125     };
126 
127     sp<DisplayInfoListener> mDisplayInfoListener;
128     const WindowListenerConsumer mUnregisterWindowInfosListener;
129 
130     const ui::Transform& getTransformForDisplayLocked(int displayId) const REQUIRES(getLock());
131 
132     void clearSpotsLocked() REQUIRES(getLock());
133 };
134 
135 } // namespace android
136 
137 #endif // _UI_POINTER_CONTROLLER_H
138