blob: b9e4ce7e7ed01883ed371bacb217a3e0c51d1324 [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
Jeff Brownb4ff35d2011-01-02 16:37:43 -080022#include <ui/DisplayInfo.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070023#include <input/Input.h>
Michael Wrightd6b473712014-02-10 15:56:36 -080024#include <inputflinger/PointerControllerInterface.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070025#include <utils/BitSet.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080026#include <utils/RefBase.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080027#include <utils/Looper.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080028#include <utils/String8.h>
29
Jeff Brownb4ff35d2011-01-02 16:37:43 -080030#include <SkBitmap.h>
31
32namespace android {
33
Jeff Brown2352b972011-04-12 22:39:53 -070034/*
35 * Pointer resources.
36 */
37struct PointerResources {
38 SpriteIcon spotHover;
39 SpriteIcon spotTouch;
40 SpriteIcon spotAnchor;
41};
42
43
44/*
45 * Pointer controller policy interface.
46 *
47 * The pointer controller policy is used by the pointer controller to interact with
48 * the Window Manager and other system components.
49 *
50 * The actual implementation is partially supported by callbacks into the DVM
51 * via JNI. This interface is also mocked in the unit tests.
52 */
53class PointerControllerPolicyInterface : public virtual RefBase {
54protected:
55 PointerControllerPolicyInterface() { }
56 virtual ~PointerControllerPolicyInterface() { }
57
58public:
59 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080060};
61
62
63/*
64 * Tracks pointer movements and draws the pointer sprite to a surface.
65 *
66 * Handles pointer acceleration and animation.
67 */
Jeff Brown05dc66a2011-03-02 14:41:58 -080068class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080069protected:
70 virtual ~PointerController();
71
72public:
Jeff Brown2352b972011-04-12 22:39:53 -070073 enum InactivityTimeout {
74 INACTIVITY_TIMEOUT_NORMAL = 0,
75 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080076 };
77
Jeff Brown2352b972011-04-12 22:39:53 -070078 PointerController(const sp<PointerControllerPolicyInterface>& policy,
79 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080080
81 virtual bool getBounds(float* outMinX, float* outMinY,
82 float* outMaxX, float* outMaxY) const;
83 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070084 virtual void setButtonState(int32_t buttonState);
85 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080086 virtual void setPosition(float x, float y);
87 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -070088 virtual void fade(Transition transition);
89 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080090
Jeff Brown2352b972011-04-12 22:39:53 -070091 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -070092 virtual void setSpots(const PointerCoords* spotCoords,
93 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -070094 virtual void clearSpots();
95
Jeff Brownd728bf52012-09-08 18:05:28 -070096 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -070097 void setPointerIcon(const SpriteIcon& icon);
98 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080099
100private:
Jeff Brown2352b972011-04-12 22:39:53 -0700101 static const size_t MAX_RECYCLED_SPRITES = 12;
102 static const size_t MAX_SPOTS = 12;
103
Jeff Brown05dc66a2011-03-02 14:41:58 -0800104 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700105 MSG_ANIMATE,
106 MSG_INACTIVITY_TIMEOUT,
107 };
108
109 struct Spot {
110 static const uint32_t INVALID_ID = 0xffffffff;
111
112 uint32_t id;
113 sp<Sprite> sprite;
114 float alpha;
115 float scale;
116 float x, y;
117
118 inline Spot(uint32_t id, const sp<Sprite>& sprite)
119 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
120 x(0.0f), y(0.0f), lastIcon(NULL) { }
121
122 void updateSprite(const SpriteIcon* icon, float x, float y);
123
124 private:
125 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800126 };
127
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800128 mutable Mutex mLock;
129
Jeff Brown2352b972011-04-12 22:39:53 -0700130 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800131 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700132 sp<SpriteController> mSpriteController;
133 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800134
Jeff Brown2352b972011-04-12 22:39:53 -0700135 PointerResources mResources;
136
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800137 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700138 bool animationPending;
139 nsecs_t animationTime;
140
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800141 int32_t displayWidth;
142 int32_t displayHeight;
143 int32_t displayOrientation;
144
Jeff Brown2352b972011-04-12 22:39:53 -0700145 InactivityTimeout inactivityTimeout;
146
147 Presentation presentation;
148 bool presentationChanged;
149
Jeff Brown538881e2011-05-25 18:23:38 -0700150 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800151 float pointerX;
152 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700153 float pointerAlpha;
154 sp<Sprite> pointerSprite;
155 SpriteIcon pointerIcon;
156 bool pointerIconChanged;
157
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800159
Jeff Brown2352b972011-04-12 22:39:53 -0700160 Vector<Spot*> spots;
161 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700162 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800163
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800164 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
165 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800166
167 void handleMessage(const Message& message);
Jeff Brown2352b972011-04-12 22:39:53 -0700168 void doAnimate();
169 void doInactivityTimeout();
170
171 void startAnimationLocked();
172
173 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700174 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700175 void updatePointerLocked();
176
177 Spot* getSpotLocked(uint32_t id);
178 Spot* createAndAddSpotLocked(uint32_t id);
179 Spot* removeFirstFadingSpotLocked();
180 void releaseSpotLocked(Spot* spot);
181 void fadeOutAndReleaseSpotLocked(Spot* spot);
182 void fadeOutAndReleaseAllSpotsLocked();
183
184 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800185};
186
187} // namespace android
188
189#endif // _UI_POINTER_CONTROLLER_H