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<PointerIconStyle, SpriteIcon>* outResources,
79             std::map<PointerIconStyle, PointerAnimation>* outAnimationResources,
80             int32_t displayId) = 0;
81     virtual PointerIconStyle getDefaultPointerIconId() = 0;
82     virtual PointerIconStyle getDefaultStylusIconId() = 0;
83     virtual PointerIconStyle getCustomPointerIconId() = 0;
84     virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position) = 0;
85 };
86 
87 /*
88  * Contains logic and resources shared among PointerController,
89  * MouseCursorController, and TouchSpotController.
90  */
91 
92 class PointerControllerContext {
93 public:
94     PointerControllerContext(const sp<PointerControllerPolicyInterface>& policy,
95                              const sp<Looper>& looper, const sp<SpriteController>& spriteController,
96                              PointerController& controller);
97     ~PointerControllerContext();
98 
99     void removeInactivityTimeout();
100     void resetInactivityTimeout();
101     void startAnimation();
102     void setInactivityTimeout(InactivityTimeout inactivityTimeout);
103 
104     nsecs_t getAnimationTime();
105 
106     void clearSpotsByDisplay(int32_t displayId);
107 
108     void setHandlerController(std::shared_ptr<PointerController> controller);
109     void setCallbackController(std::shared_ptr<PointerController> controller);
110 
111     sp<PointerControllerPolicyInterface> getPolicy();
112     sp<SpriteController> getSpriteController();
113 
114     void handleDisplayEvents();
115 
116     void addAnimationCallback(int32_t displayId, std::function<bool(nsecs_t)> callback);
117     void removeAnimationCallback(int32_t displayId);
118 
119     class MessageHandler : public virtual android::MessageHandler {
120     public:
121         enum {
122             MSG_INACTIVITY_TIMEOUT,
123         };
124 
125         void handleMessage(const Message& message) override;
126         std::weak_ptr<PointerController> pointerController;
127     };
128 
129     class LooperCallback : public virtual android::LooperCallback {
130     public:
131         int handleEvent(int fd, int events, void* data) override;
132         std::weak_ptr<PointerController> pointerController;
133     };
134 
135 private:
136     class PointerAnimator {
137     public:
138         PointerAnimator(PointerControllerContext& context);
139 
140         void addCallback(int32_t displayId, std::function<bool(nsecs_t)> callback);
141         void removeCallback(int32_t displayId);
142         void handleVsyncEvents();
143         nsecs_t getAnimationTimeLocked();
144 
145         mutable std::mutex mLock;
146 
147     private:
148         struct Locked {
149             bool animationPending{false};
150             nsecs_t animationTime{systemTime(SYSTEM_TIME_MONOTONIC)};
151 
152             std::unordered_map<int32_t, std::function<bool(nsecs_t)>> callbacks;
153         } mLocked GUARDED_BY(mLock);
154 
155         DisplayEventReceiver mDisplayEventReceiver;
156 
157         PointerControllerContext& mContext;
158 
159         void initializeDisplayEventReceiver();
160         void startAnimationLocked();
161         void handleCallbacksLocked(nsecs_t timestamp);
162     };
163 
164     sp<PointerControllerPolicyInterface> mPolicy;
165     sp<Looper> mLooper;
166     sp<SpriteController> mSpriteController;
167     sp<MessageHandler> mHandler;
168     sp<LooperCallback> mCallback;
169 
170     PointerController& mController;
171 
172     PointerAnimator mAnimator;
173 
174     mutable std::mutex mLock;
175 
176     struct Locked {
177         InactivityTimeout inactivityTimeout;
178     } mLocked GUARDED_BY(mLock);
179 
180     void resetInactivityTimeoutLocked();
181 };
182 
183 } // namespace android
184 
185 #endif // _UI_POINTER_CONTROLLER_CONTEXT_H
186