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