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