blob: 14c0679654c699a0640854eab2950259a099d489 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 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_H
18#define _UI_POINTER_CONTROLLER_H
19
Michael Wrighta0bc6b12020-06-26 20:25:34 +010020#include <PointerControllerInterface.h>
21#include <gui/DisplayEventReceiver.h>
Arthur Hungb9b32002018-12-18 17:39:43 +080022#include <input/DisplayViewport.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070023#include <input/Input.h>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010024#include <ui/DisplayInfo.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070025#include <utils/BitSet.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080026#include <utils/Looper.h>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010027#include <utils/RefBase.h>
28
29#include <map>
30#include <memory>
31#include <vector>
32
33#include "SpriteController.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080034
Jeff Brownb4ff35d2011-01-02 16:37:43 -080035namespace android {
36
Jeff Brown2352b972011-04-12 22:39:53 -070037/*
38 * Pointer resources.
39 */
40struct PointerResources {
41 SpriteIcon spotHover;
42 SpriteIcon spotTouch;
43 SpriteIcon spotAnchor;
44};
45
Jun Mukai808196f2015-10-28 16:46:44 -070046struct PointerAnimation {
47 std::vector<SpriteIcon> animationFrames;
48 nsecs_t durationPerFrame;
49};
50
Jeff Brown2352b972011-04-12 22:39:53 -070051/*
52 * Pointer controller policy interface.
53 *
54 * The pointer controller policy is used by the pointer controller to interact with
55 * the Window Manager and other system components.
56 *
57 * The actual implementation is partially supported by callbacks into the DVM
58 * via JNI. This interface is also mocked in the unit tests.
59 */
60class PointerControllerPolicyInterface : public virtual RefBase {
61protected:
62 PointerControllerPolicyInterface() { }
63 virtual ~PointerControllerPolicyInterface() { }
64
65public:
Andrii Kulianfd8666d2018-10-05 16:58:39 -070066 virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0;
67 virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0;
Jun Mukai808196f2015-10-28 16:46:44 -070068 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
Andrii Kulianfd8666d2018-10-05 16:58:39 -070069 std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) = 0;
Jun Mukai5ec74202015-10-07 16:58:09 +090070 virtual int32_t getDefaultPointerIconId() = 0;
Jun Mukaid4eaef72015-10-30 15:54:33 -070071 virtual int32_t getCustomPointerIconId() = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080072};
73
Jeff Brownb4ff35d2011-01-02 16:37:43 -080074/*
75 * Tracks pointer movements and draws the pointer sprite to a surface.
76 *
77 * Handles pointer acceleration and animation.
78 */
Michael Wrighta0bc6b12020-06-26 20:25:34 +010079class PointerController : public PointerControllerInterface {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080080public:
Michael Wrighta0bc6b12020-06-26 20:25:34 +010081 static std::shared_ptr<PointerController> create(
82 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
83 const sp<SpriteController>& spriteController);
Michael Wright6853fe62020-07-02 00:01:38 +010084 enum class InactivityTimeout {
85 NORMAL = 0,
86 SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080087 };
88
Michael Wrighta0bc6b12020-06-26 20:25:34 +010089 virtual ~PointerController();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080090
91 virtual bool getBounds(float* outMinX, float* outMinY,
92 float* outMaxX, float* outMaxY) const;
93 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070094 virtual void setButtonState(int32_t buttonState);
95 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080096 virtual void setPosition(float x, float y);
97 virtual void getPosition(float* outX, float* outY) const;
Arthur Hungb9b32002018-12-18 17:39:43 +080098 virtual int32_t getDisplayId() const;
Jeff Brown538881e2011-05-25 18:23:38 -070099 virtual void fade(Transition transition);
100 virtual void unfade(Transition transition);
Garfield Tanb1b07be2020-01-09 11:30:04 -0800101 virtual void setDisplayViewport(const DisplayViewport& viewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800102
Jeff Brown2352b972011-04-12 22:39:53 -0700103 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700104 virtual void setSpots(const PointerCoords* spotCoords,
Arthur Hungd25699a2019-01-25 17:53:22 +0800105 const uint32_t* spotIdToIndex, BitSet32 spotIdBits, int32_t displayId);
Jeff Brown2352b972011-04-12 22:39:53 -0700106 virtual void clearSpots();
107
Michael Wrightf9d9ce772016-05-13 17:44:16 +0100108 void updatePointerIcon(int32_t iconId);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700109 void setCustomPointerIcon(const SpriteIcon& icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700110 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jun Mukai19a56012015-11-24 11:25:52 -0800111 void reloadPointerResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800112
113private:
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100114 static constexpr size_t MAX_RECYCLED_SPRITES = 12;
115 static constexpr size_t MAX_SPOTS = 12;
Jeff Brown2352b972011-04-12 22:39:53 -0700116
Jeff Brown05dc66a2011-03-02 14:41:58 -0800117 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700118 MSG_INACTIVITY_TIMEOUT,
119 };
120
121 struct Spot {
122 static const uint32_t INVALID_ID = 0xffffffff;
123
124 uint32_t id;
125 sp<Sprite> sprite;
126 float alpha;
127 float scale;
128 float x, y;
129
130 inline Spot(uint32_t id, const sp<Sprite>& sprite)
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100131 : id(id),
132 sprite(sprite),
133 alpha(1.0f),
134 scale(1.0f),
135 x(0.0f),
136 y(0.0f),
137 lastIcon(nullptr) {}
Jeff Brown2352b972011-04-12 22:39:53 -0700138
Arthur Hungd25699a2019-01-25 17:53:22 +0800139 void updateSprite(const SpriteIcon* icon, float x, float y, int32_t displayId);
Jeff Brown2352b972011-04-12 22:39:53 -0700140
141 private:
142 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800143 };
144
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100145 class MessageHandler : public virtual android::MessageHandler {
146 public:
147 void handleMessage(const Message& message) override;
148 std::weak_ptr<PointerController> pointerController;
149 };
150
151 class LooperCallback : public virtual android::LooperCallback {
152 public:
153 int handleEvent(int fd, int events, void* data) override;
154 std::weak_ptr<PointerController> pointerController;
155 };
156
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800157 mutable Mutex mLock;
158
Jeff Brown2352b972011-04-12 22:39:53 -0700159 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800160 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700161 sp<SpriteController> mSpriteController;
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100162 sp<MessageHandler> mHandler;
Vladislav Kaznacheev33c59032016-09-09 10:03:31 -0700163 sp<LooperCallback> mCallback;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800164
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700165 DisplayEventReceiver mDisplayEventReceiver;
166
Jeff Brown2352b972011-04-12 22:39:53 -0700167 PointerResources mResources;
168
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800169 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700170 bool animationPending;
171 nsecs_t animationTime;
172
Jun Mukai808196f2015-10-28 16:46:44 -0700173 size_t animationFrameIndex;
174 nsecs_t lastFrameUpdatedTime;
175
Arthur Hungb9b32002018-12-18 17:39:43 +0800176 DisplayViewport viewport;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800177
Jeff Brown2352b972011-04-12 22:39:53 -0700178 InactivityTimeout inactivityTimeout;
179
180 Presentation presentation;
181 bool presentationChanged;
182
Jeff Brown538881e2011-05-25 18:23:38 -0700183 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800184 float pointerX;
185 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700186 float pointerAlpha;
187 sp<Sprite> pointerSprite;
188 SpriteIcon pointerIcon;
189 bool pointerIconChanged;
190
Jun Mukai808196f2015-10-28 16:46:44 -0700191 std::map<int32_t, SpriteIcon> additionalMouseResources;
192 std::map<int32_t, PointerAnimation> animationResources;
Jun Mukai1db53972015-09-11 18:08:31 -0700193
Michael Wrightf9d9ce772016-05-13 17:44:16 +0100194 int32_t requestedPointerType;
Jun Mukai1db53972015-09-11 18:08:31 -0700195
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700196 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800197
Arthur Hungd25699a2019-01-25 17:53:22 +0800198 std::map<int32_t /* displayId */, std::vector<Spot*>> spotsByDisplay;
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100199 std::vector<sp<Sprite>> recycledSprites;
Arthur Hungb9b32002018-12-18 17:39:43 +0800200 } mLocked GUARDED_BY(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800201
Michael Wrighta0bc6b12020-06-26 20:25:34 +0100202 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
203 const sp<SpriteController>& spriteController);
204
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800205 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
206 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800207
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700208 void doAnimate(nsecs_t timestamp);
Jun Mukai808196f2015-10-28 16:46:44 -0700209 bool doFadingAnimationLocked(nsecs_t timestamp);
210 bool doBitmapAnimationLocked(nsecs_t timestamp);
Jeff Brown2352b972011-04-12 22:39:53 -0700211 void doInactivityTimeout();
212
213 void startAnimationLocked();
214
215 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700216 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700217 void updatePointerLocked();
218
Arthur Hungd25699a2019-01-25 17:53:22 +0800219 Spot* getSpot(uint32_t id, const std::vector<Spot*>& spots);
220 Spot* createAndAddSpotLocked(uint32_t id, std::vector<Spot*>& spots);
221 Spot* removeFirstFadingSpotLocked(std::vector<Spot*>& spots);
Jeff Brown2352b972011-04-12 22:39:53 -0700222 void releaseSpotLocked(Spot* spot);
223 void fadeOutAndReleaseSpotLocked(Spot* spot);
224 void fadeOutAndReleaseAllSpotsLocked();
225
Arthur Hungb9b32002018-12-18 17:39:43 +0800226 void loadResourcesLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800227};
228
229} // namespace android
230
231#endif // _UI_POINTER_CONTROLLER_H