blob: 7f4e5a59c9b6a1217f2ac9a2c601ef544ed16f33 [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
Jeff Brown5541de92011-04-11 11:54:25 -070020#include "SpriteController.h"
21
Jun Mukai1db53972015-09-11 18:08:31 -070022#include <map>
Jun Mukai808196f2015-10-28 16:46:44 -070023#include <vector>
Jun Mukai1db53972015-09-11 18:08:31 -070024
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070026#include <input/Input.h>
Prabir Pradhane5696a52018-11-14 19:55:21 -080027#include <PointerControllerInterface.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070028#include <utils/BitSet.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080029#include <utils/RefBase.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080030#include <utils/Looper.h>
Jun Mukaic0c0ac32015-10-27 10:09:21 -070031#include <gui/DisplayEventReceiver.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033namespace android {
34
Jeff Brown2352b972011-04-12 22:39:53 -070035/*
36 * Pointer resources.
37 */
38struct PointerResources {
39 SpriteIcon spotHover;
40 SpriteIcon spotTouch;
41 SpriteIcon spotAnchor;
42};
43
Jun Mukai808196f2015-10-28 16:46:44 -070044struct PointerAnimation {
45 std::vector<SpriteIcon> animationFrames;
46 nsecs_t durationPerFrame;
47};
48
Jeff Brown2352b972011-04-12 22:39:53 -070049/*
50 * Pointer controller policy interface.
51 *
52 * The pointer controller policy is used by the pointer controller to interact with
53 * the Window Manager and other system components.
54 *
55 * The actual implementation is partially supported by callbacks into the DVM
56 * via JNI. This interface is also mocked in the unit tests.
57 */
58class PointerControllerPolicyInterface : public virtual RefBase {
59protected:
60 PointerControllerPolicyInterface() { }
61 virtual ~PointerControllerPolicyInterface() { }
62
63public:
Jun Mukai19a56012015-11-24 11:25:52 -080064 virtual void loadPointerIcon(SpriteIcon* icon) = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070065 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jun Mukai808196f2015-10-28 16:46:44 -070066 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
67 std::map<int32_t, PointerAnimation>* outAnimationResources) = 0;
Jun Mukai5ec74202015-10-07 16:58:09 +090068 virtual int32_t getDefaultPointerIconId() = 0;
Jun Mukaid4eaef72015-10-30 15:54:33 -070069 virtual int32_t getCustomPointerIconId() = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080070};
71
72
73/*
74 * Tracks pointer movements and draws the pointer sprite to a surface.
75 *
76 * Handles pointer acceleration and animation.
77 */
Jun Mukaic0c0ac32015-10-27 10:09:21 -070078class PointerController : public PointerControllerInterface, public MessageHandler,
79 public LooperCallback {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080080protected:
81 virtual ~PointerController();
82
83public:
Jeff Brown2352b972011-04-12 22:39:53 -070084 enum InactivityTimeout {
85 INACTIVITY_TIMEOUT_NORMAL = 0,
86 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080087 };
88
Jeff Brown2352b972011-04-12 22:39:53 -070089 PointerController(const sp<PointerControllerPolicyInterface>& policy,
90 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080091
92 virtual bool getBounds(float* outMinX, float* outMinY,
93 float* outMaxX, float* outMaxY) const;
94 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070095 virtual void setButtonState(int32_t buttonState);
96 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080097 virtual void setPosition(float x, float y);
98 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -070099 virtual void fade(Transition transition);
100 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800101
Jeff Brown2352b972011-04-12 22:39:53 -0700102 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700103 virtual void setSpots(const PointerCoords* spotCoords,
104 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700105 virtual void clearSpots();
106
Michael Wrightf9d9ce772016-05-13 17:44:16 +0100107 void updatePointerIcon(int32_t iconId);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700108 void setCustomPointerIcon(const SpriteIcon& icon);
Jeff Brownd728bf52012-09-08 18:05:28 -0700109 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
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:
Jeff Brown2352b972011-04-12 22:39:53 -0700114 static const size_t MAX_RECYCLED_SPRITES = 12;
115 static const size_t MAX_SPOTS = 12;
116
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)
131 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
132 x(0.0f), y(0.0f), lastIcon(NULL) { }
133
134 void updateSprite(const SpriteIcon* icon, float x, float y);
135
136 private:
137 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800138 };
139
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800140 mutable Mutex mLock;
141
Jeff Brown2352b972011-04-12 22:39:53 -0700142 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800143 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700144 sp<SpriteController> mSpriteController;
145 sp<WeakMessageHandler> mHandler;
Vladislav Kaznacheev33c59032016-09-09 10:03:31 -0700146 sp<LooperCallback> mCallback;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800147
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700148 DisplayEventReceiver mDisplayEventReceiver;
149
Jeff Brown2352b972011-04-12 22:39:53 -0700150 PointerResources mResources;
151
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800152 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700153 bool animationPending;
154 nsecs_t animationTime;
155
Jun Mukai808196f2015-10-28 16:46:44 -0700156 size_t animationFrameIndex;
157 nsecs_t lastFrameUpdatedTime;
158
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800159 int32_t displayWidth;
160 int32_t displayHeight;
161 int32_t displayOrientation;
162
Jeff Brown2352b972011-04-12 22:39:53 -0700163 InactivityTimeout inactivityTimeout;
164
165 Presentation presentation;
166 bool presentationChanged;
167
Jeff Brown538881e2011-05-25 18:23:38 -0700168 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800169 float pointerX;
170 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700171 float pointerAlpha;
172 sp<Sprite> pointerSprite;
173 SpriteIcon pointerIcon;
174 bool pointerIconChanged;
175
Jun Mukai808196f2015-10-28 16:46:44 -0700176 std::map<int32_t, SpriteIcon> additionalMouseResources;
177 std::map<int32_t, PointerAnimation> animationResources;
Jun Mukai1db53972015-09-11 18:08:31 -0700178
Michael Wrightf9d9ce772016-05-13 17:44:16 +0100179 int32_t requestedPointerType;
Jun Mukai1db53972015-09-11 18:08:31 -0700180
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700181 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800182
Jeff Brown2352b972011-04-12 22:39:53 -0700183 Vector<Spot*> spots;
184 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700185 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800186
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800187 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
188 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800189
190 void handleMessage(const Message& message);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700191 int handleEvent(int fd, int events, void* data);
192 void doAnimate(nsecs_t timestamp);
Jun Mukai808196f2015-10-28 16:46:44 -0700193 bool doFadingAnimationLocked(nsecs_t timestamp);
194 bool doBitmapAnimationLocked(nsecs_t timestamp);
Jeff Brown2352b972011-04-12 22:39:53 -0700195 void doInactivityTimeout();
196
197 void startAnimationLocked();
198
199 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700200 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700201 void updatePointerLocked();
202
203 Spot* getSpotLocked(uint32_t id);
204 Spot* createAndAddSpotLocked(uint32_t id);
205 Spot* removeFirstFadingSpotLocked();
206 void releaseSpotLocked(Spot* spot);
207 void fadeOutAndReleaseSpotLocked(Spot* spot);
208 void fadeOutAndReleaseAllSpotsLocked();
209
210 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800211};
212
213} // namespace android
214
215#endif // _UI_POINTER_CONTROLLER_H