• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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_CONTEXT_H
18 #define _UI_POINTER_CONTROLLER_CONTEXT_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 <functional>
29 #include <map>
30 #include <memory>
31 #include <vector>
32 
33 #include "SpriteController.h"
34 
35 namespace android {
36 
37 class PointerController;
38 class MouseCursorController;
39 class TouchSpotController;
40 
41 /*
42  * Pointer resources.
43  */
44 struct PointerResources {
45     SpriteIcon spotHover;
46     SpriteIcon spotTouch;
47     SpriteIcon spotAnchor;
48 };
49 
50 struct PointerAnimation {
51     std::vector<SpriteIcon> animationFrames;
52     nsecs_t durationPerFrame;
53 };
54 
55 enum class InactivityTimeout {
56     NORMAL = 0,
57     SHORT = 1,
58 };
59 
60 /*
61  * Pointer controller policy interface.
62  *
63  * The pointer controller policy is used by the pointer controller to interact with
64  * the Window Manager and other system components.
65  *
66  * The actual implementation is partially supported by callbacks into the DVM
67  * via JNI.  This interface is also mocked in the unit tests.
68  */
69 class PointerControllerPolicyInterface : public virtual RefBase {
70 protected:
PointerControllerPolicyInterface()71     PointerControllerPolicyInterface() {}
~PointerControllerPolicyInterface()72     virtual ~PointerControllerPolicyInterface() {}
73 
74 public:
75     virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0;
76     virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0;
77     virtual void loadAdditionalMouseResources(
78             std::map<int32_t, SpriteIcon>* outResources,
79             std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) = 0;
80     virtual int32_t getDefaultPointerIconId() = 0;
81     virtual int32_t getCustomPointerIconId() = 0;
82 };
83 
84 /*
85  * Contains logic and resources shared among PointerController,
86  * MouseCursorController, and TouchSpotController.
87  */
88 
89 class PointerControllerContext {
90 public:
91     PointerControllerContext(const sp<PointerControllerPolicyInterface>& policy,
92                              const sp<Looper>& looper, const sp<SpriteController>& spriteController,
93                              PointerController& controller);
94     ~PointerControllerContext();
95 
96     void removeInactivityTimeout();
97     void resetInactivityTimeout();
98     void startAnimation();
99     void setInactivityTimeout(InactivityTimeout inactivityTimeout);
100 
101     nsecs_t getAnimationTime();
102 
103     void clearSpotsByDisplay(int32_t displayId);
104 
105     void setHandlerController(std::shared_ptr<PointerController> controller);
106     void setCallbackController(std::shared_ptr<PointerController> controller);
107 
108     sp<PointerControllerPolicyInterface> getPolicy();
109     sp<SpriteController> getSpriteController();
110 
111     void handleDisplayEvents();
112 
113     void addAnimationCallback(int32_t displayId, std::function<bool(nsecs_t)> callback);
114     void removeAnimationCallback(int32_t displayId);
115 
116     class MessageHandler : public virtual android::MessageHandler {
117     public:
118         enum {
119             MSG_INACTIVITY_TIMEOUT,
120         };
121 
122         void handleMessage(const Message& message) override;
123         std::weak_ptr<PointerController> pointerController;
124     };
125 
126     class LooperCallback : public virtual android::LooperCallback {
127     public:
128         int handleEvent(int fd, int events, void* data) override;
129         std::weak_ptr<PointerController> pointerController;
130     };
131 
132 private:
133     class PointerAnimator {
134     public:
135         PointerAnimator(PointerControllerContext& context);
136 
137         void addCallback(int32_t displayId, std::function<bool(nsecs_t)> callback);
138         void removeCallback(int32_t displayId);
139         void handleVsyncEvents();
140         nsecs_t getAnimationTimeLocked();
141 
142         mutable std::mutex mLock;
143 
144     private:
145         struct Locked {
146             bool animationPending{false};
147             nsecs_t animationTime{systemTime(SYSTEM_TIME_MONOTONIC)};
148 
149             std::unordered_map<int32_t, std::function<bool(nsecs_t)>> callbacks;
150         } mLocked GUARDED_BY(mLock);
151 
152         DisplayEventReceiver mDisplayEventReceiver;
153 
154         PointerControllerContext& mContext;
155 
156         void initializeDisplayEventReceiver();
157         void startAnimationLocked();
158         void handleCallbacksLocked(nsecs_t timestamp);
159     };
160 
161     sp<PointerControllerPolicyInterface> mPolicy;
162     sp<Looper> mLooper;
163     sp<SpriteController> mSpriteController;
164     sp<MessageHandler> mHandler;
165     sp<LooperCallback> mCallback;
166 
167     PointerController& mController;
168 
169     PointerAnimator mAnimator;
170 
171     mutable std::mutex mLock;
172 
173     struct Locked {
174         InactivityTimeout inactivityTimeout;
175     } mLocked GUARDED_BY(mLock);
176 
177     void resetInactivityTimeoutLocked();
178 };
179 
180 } // namespace android
181 
182 #endif // _UI_POINTER_CONTROLLER_CONTEXT_H
183