blob: 1797428b343f6ae01134b40377dbca77cbf9ed5c [file] [log] [blame]
Liam Harringtonc782be62020-07-17 19:48:24 +00001/*
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>
Liam Harringtonc782be62020-07-17 19:48:24 +000024#include <utils/BitSet.h>
25#include <utils/Looper.h>
26#include <utils/RefBase.h>
27
Liam Harringtonce637132020-08-14 04:00:11 +000028#include <functional>
Liam Harringtonc782be62020-07-17 19:48:24 +000029#include <map>
30#include <memory>
31#include <vector>
32
33#include "SpriteController.h"
34
35namespace android {
36
37class PointerController;
Liam Harringtonce637132020-08-14 04:00:11 +000038class MouseCursorController;
39class TouchSpotController;
Liam Harringtonc782be62020-07-17 19:48:24 +000040
41/*
42 * Pointer resources.
43 */
44struct PointerResources {
45 SpriteIcon spotHover;
46 SpriteIcon spotTouch;
47 SpriteIcon spotAnchor;
48};
49
50struct PointerAnimation {
51 std::vector<SpriteIcon> animationFrames;
52 nsecs_t durationPerFrame;
53};
54
55enum 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 */
69class PointerControllerPolicyInterface : public virtual RefBase {
70protected:
71 PointerControllerPolicyInterface() {}
72 virtual ~PointerControllerPolicyInterface() {}
73
74public:
75 virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0;
76 virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0;
77 virtual void loadAdditionalMouseResources(
Brandon Pollack015f5d92022-06-02 06:59:33 +000078 std::map<PointerIconStyle, SpriteIcon>* outResources,
79 std::map<PointerIconStyle, PointerAnimation>* outAnimationResources,
80 int32_t displayId) = 0;
81 virtual PointerIconStyle getDefaultPointerIconId() = 0;
82 virtual PointerIconStyle getCustomPointerIconId() = 0;
Prabir Pradhan0e3d6652022-03-10 14:39:46 +000083 virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) = 0;
Liam Harringtonc782be62020-07-17 19:48:24 +000084};
85
86/*
87 * Contains logic and resources shared among PointerController,
88 * MouseCursorController, and TouchSpotController.
89 */
90
91class PointerControllerContext {
92public:
93 PointerControllerContext(const sp<PointerControllerPolicyInterface>& policy,
94 const sp<Looper>& looper, const sp<SpriteController>& spriteController,
95 PointerController& controller);
96 ~PointerControllerContext();
97
98 void removeInactivityTimeout();
99 void resetInactivityTimeout();
100 void startAnimation();
101 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
102
Liam Harringtonc782be62020-07-17 19:48:24 +0000103 nsecs_t getAnimationTime();
104
105 void clearSpotsByDisplay(int32_t displayId);
106
107 void setHandlerController(std::shared_ptr<PointerController> controller);
108 void setCallbackController(std::shared_ptr<PointerController> controller);
109
110 sp<PointerControllerPolicyInterface> getPolicy();
111 sp<SpriteController> getSpriteController();
112
Liam Harringtonc782be62020-07-17 19:48:24 +0000113 void handleDisplayEvents();
114
Liam Harringtonce637132020-08-14 04:00:11 +0000115 void addAnimationCallback(int32_t displayId, std::function<bool(nsecs_t)> callback);
116 void removeAnimationCallback(int32_t displayId);
117
Liam Harringtonc782be62020-07-17 19:48:24 +0000118 class MessageHandler : public virtual android::MessageHandler {
119 public:
120 enum {
121 MSG_INACTIVITY_TIMEOUT,
122 };
123
124 void handleMessage(const Message& message) override;
125 std::weak_ptr<PointerController> pointerController;
126 };
127
128 class LooperCallback : public virtual android::LooperCallback {
129 public:
130 int handleEvent(int fd, int events, void* data) override;
131 std::weak_ptr<PointerController> pointerController;
132 };
133
134private:
Liam Harringtonce637132020-08-14 04:00:11 +0000135 class PointerAnimator {
136 public:
137 PointerAnimator(PointerControllerContext& context);
138
139 void addCallback(int32_t displayId, std::function<bool(nsecs_t)> callback);
140 void removeCallback(int32_t displayId);
141 void handleVsyncEvents();
142 nsecs_t getAnimationTimeLocked();
143
144 mutable std::mutex mLock;
145
146 private:
147 struct Locked {
148 bool animationPending{false};
149 nsecs_t animationTime{systemTime(SYSTEM_TIME_MONOTONIC)};
150
151 std::unordered_map<int32_t, std::function<bool(nsecs_t)>> callbacks;
152 } mLocked GUARDED_BY(mLock);
153
154 DisplayEventReceiver mDisplayEventReceiver;
155
156 PointerControllerContext& mContext;
157
158 void initializeDisplayEventReceiver();
159 void startAnimationLocked();
160 void handleCallbacksLocked(nsecs_t timestamp);
161 };
162
Liam Harringtonc782be62020-07-17 19:48:24 +0000163 sp<PointerControllerPolicyInterface> mPolicy;
164 sp<Looper> mLooper;
165 sp<SpriteController> mSpriteController;
166 sp<MessageHandler> mHandler;
167 sp<LooperCallback> mCallback;
168
Liam Harringtonc782be62020-07-17 19:48:24 +0000169 PointerController& mController;
170
Liam Harringtonce637132020-08-14 04:00:11 +0000171 PointerAnimator mAnimator;
172
Liam Harringtonc782be62020-07-17 19:48:24 +0000173 mutable std::mutex mLock;
174
175 struct Locked {
Liam Harringtonc782be62020-07-17 19:48:24 +0000176 InactivityTimeout inactivityTimeout;
177 } mLocked GUARDED_BY(mLock);
178
179 void resetInactivityTimeoutLocked();
180};
181
182} // namespace android
183
184#endif // _UI_POINTER_CONTROLLER_CONTEXT_H