blob: 4fd2d8503272a88f248dfde442e1ff35ce8a8b98 [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>
Michael Wrightd6b473712014-02-10 15:56:36 -080027#include <inputflinger/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>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080031#include <utils/String8.h>
Jun Mukaic0c0ac32015-10-27 10:09:21 -070032#include <gui/DisplayEventReceiver.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033
Jeff Brownb4ff35d2011-01-02 16:37:43 -080034namespace android {
35
Jeff Brown2352b972011-04-12 22:39:53 -070036/*
37 * Pointer resources.
38 */
39struct PointerResources {
40 SpriteIcon spotHover;
41 SpriteIcon spotTouch;
42 SpriteIcon spotAnchor;
43};
44
Jun Mukai808196f2015-10-28 16:46:44 -070045struct PointerAnimation {
46 std::vector<SpriteIcon> animationFrames;
47 nsecs_t durationPerFrame;
48};
49
Jeff Brown2352b972011-04-12 22:39:53 -070050/*
51 * Pointer controller policy interface.
52 *
53 * The pointer controller policy is used by the pointer controller to interact with
54 * the Window Manager and other system components.
55 *
56 * The actual implementation is partially supported by callbacks into the DVM
57 * via JNI. This interface is also mocked in the unit tests.
58 */
59class PointerControllerPolicyInterface : public virtual RefBase {
60protected:
61 PointerControllerPolicyInterface() { }
62 virtual ~PointerControllerPolicyInterface() { }
63
64public:
Jun Mukai19a56012015-11-24 11:25:52 -080065 virtual void loadPointerIcon(SpriteIcon* icon) = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070066 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jun Mukai808196f2015-10-28 16:46:44 -070067 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
68 std::map<int32_t, PointerAnimation>* outAnimationResources) = 0;
Jun Mukai5ec74202015-10-07 16:58:09 +090069 virtual int32_t getDefaultPointerIconId() = 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
Jun Mukai808196f2015-10-28 16:46:44 -0700107 void updatePointerShape(int32_t iconId);
Jeff Brownd728bf52012-09-08 18:05:28 -0700108 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700109 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jun Mukai19a56012015-11-24 11:25:52 -0800110 void reloadPointerResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800111
112private:
Jeff Brown2352b972011-04-12 22:39:53 -0700113 static const size_t MAX_RECYCLED_SPRITES = 12;
114 static const size_t MAX_SPOTS = 12;
115
Jeff Brown05dc66a2011-03-02 14:41:58 -0800116 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700117 MSG_INACTIVITY_TIMEOUT,
118 };
119
120 struct Spot {
121 static const uint32_t INVALID_ID = 0xffffffff;
122
123 uint32_t id;
124 sp<Sprite> sprite;
125 float alpha;
126 float scale;
127 float x, y;
128
129 inline Spot(uint32_t id, const sp<Sprite>& sprite)
130 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
131 x(0.0f), y(0.0f), lastIcon(NULL) { }
132
133 void updateSprite(const SpriteIcon* icon, float x, float y);
134
135 private:
136 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800137 };
138
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800139 mutable Mutex mLock;
140
Jeff Brown2352b972011-04-12 22:39:53 -0700141 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800142 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700143 sp<SpriteController> mSpriteController;
144 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700146 DisplayEventReceiver mDisplayEventReceiver;
147
Jeff Brown2352b972011-04-12 22:39:53 -0700148 PointerResources mResources;
149
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800150 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700151 bool animationPending;
152 nsecs_t animationTime;
153
Jun Mukai808196f2015-10-28 16:46:44 -0700154 size_t animationFrameIndex;
155 nsecs_t lastFrameUpdatedTime;
156
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800157 int32_t displayWidth;
158 int32_t displayHeight;
159 int32_t displayOrientation;
160
Jeff Brown2352b972011-04-12 22:39:53 -0700161 InactivityTimeout inactivityTimeout;
162
163 Presentation presentation;
164 bool presentationChanged;
165
Jeff Brown538881e2011-05-25 18:23:38 -0700166 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800167 float pointerX;
168 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700169 float pointerAlpha;
170 sp<Sprite> pointerSprite;
171 SpriteIcon pointerIcon;
172 bool pointerIconChanged;
173
Jun Mukai808196f2015-10-28 16:46:44 -0700174 std::map<int32_t, SpriteIcon> additionalMouseResources;
175 std::map<int32_t, PointerAnimation> animationResources;
Jun Mukai1db53972015-09-11 18:08:31 -0700176
177 int32_t requestedPointerShape;
178
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700179 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800180
Jeff Brown2352b972011-04-12 22:39:53 -0700181 Vector<Spot*> spots;
182 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700183 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800184
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800185 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
186 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800187
188 void handleMessage(const Message& message);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700189 int handleEvent(int fd, int events, void* data);
190 void doAnimate(nsecs_t timestamp);
Jun Mukai808196f2015-10-28 16:46:44 -0700191 bool doFadingAnimationLocked(nsecs_t timestamp);
192 bool doBitmapAnimationLocked(nsecs_t timestamp);
Jeff Brown2352b972011-04-12 22:39:53 -0700193 void doInactivityTimeout();
194
195 void startAnimationLocked();
196
197 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700198 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700199 void updatePointerLocked();
200
201 Spot* getSpotLocked(uint32_t id);
202 Spot* createAndAddSpotLocked(uint32_t id);
203 Spot* removeFirstFadingSpotLocked();
204 void releaseSpotLocked(Spot* spot);
205 void fadeOutAndReleaseSpotLocked(Spot* spot);
206 void fadeOutAndReleaseAllSpotsLocked();
207
208 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800209};
210
211} // namespace android
212
213#endif // _UI_POINTER_CONTROLLER_H