blob: afd6371168832e8b75cc42178c28836ee25ec5ef [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 Brown05dc66a2011-03-02 14:41:58 -080023#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080024#include <utils/RefBase.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080025#include <utils/Looper.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080026#include <utils/String8.h>
27
Jeff Brownb4ff35d2011-01-02 16:37:43 -080028#include <SkBitmap.h>
29
30namespace android {
31
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032/**
Jeff Brown2352b972011-04-12 22:39:53 -070033 * Interface for tracking a mouse / touch pad pointer and touch pad spots.
34 *
35 * The spots are sprites on screen that visually represent the positions of
36 * fingers
Jeff Brownb4ff35d2011-01-02 16:37:43 -080037 *
38 * The pointer controller is responsible for providing synchronization and for tracking
39 * display orientation changes if needed.
40 */
41class PointerControllerInterface : public virtual RefBase {
42protected:
43 PointerControllerInterface() { }
44 virtual ~PointerControllerInterface() { }
45
46public:
47 /* Gets the bounds of the region that the pointer can traverse.
48 * Returns true if the bounds are available. */
49 virtual bool getBounds(float* outMinX, float* outMinY,
50 float* outMaxX, float* outMaxY) const = 0;
51
52 /* Move the pointer. */
53 virtual void move(float deltaX, float deltaY) = 0;
54
55 /* Sets a mask that indicates which buttons are pressed. */
56 virtual void setButtonState(uint32_t buttonState) = 0;
57
58 /* Gets a mask that indicates which buttons are pressed. */
59 virtual uint32_t getButtonState() const = 0;
60
61 /* Sets the absolute location of the pointer. */
62 virtual void setPosition(float x, float y) = 0;
63
64 /* Gets the absolute location of the pointer. */
65 virtual void getPosition(float* outX, float* outY) const = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -080066
67 /* Fades the pointer out now. */
68 virtual void fade() = 0;
69
Jeff Brown2352b972011-04-12 22:39:53 -070070 /* Makes the pointer visible if it has faded out.
71 * The pointer never unfades itself automatically. This method must be called
72 * by the client whenever the pointer is moved or a button is pressed and it
73 * wants to ensure that the pointer becomes visible again. */
Jeff Brown05dc66a2011-03-02 14:41:58 -080074 virtual void unfade() = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070075
76 enum Presentation {
77 // Show the mouse pointer.
78 PRESENTATION_POINTER,
79 // Show spots and a spot anchor in place of the mouse pointer.
80 PRESENTATION_SPOT,
81 };
82
83 /* Sets the mode of the pointer controller. */
84 virtual void setPresentation(Presentation presentation) = 0;
85
86 // Describes the current gesture.
87 enum SpotGesture {
88 // No gesture.
89 // Do not display any spots.
90 SPOT_GESTURE_NEUTRAL,
91 // Tap at current location.
92 // Briefly display one spot at the tapped location.
93 SPOT_GESTURE_TAP,
94 // Button pressed but no finger is down.
95 // Display spot at pressed location.
96 SPOT_GESTURE_BUTTON_CLICK,
97 // Button pressed and a finger is down.
98 // Display spot at pressed location.
99 SPOT_GESTURE_BUTTON_DRAG,
100 // One finger down and hovering.
101 // Display spot at the hovered location.
102 SPOT_GESTURE_HOVER,
103 // Two fingers down but not sure in which direction they are moving so we consider
104 // it a press at the pointer location.
105 // Display two spots near the pointer location.
106 SPOT_GESTURE_PRESS,
107 // Two fingers down and moving in same direction.
108 // Display two spots near the pointer location.
109 SPOT_GESTURE_SWIPE,
110 // Two or more fingers down and moving in arbitrary directions.
111 // Display two or more spots near the pointer location, one for each finger.
112 SPOT_GESTURE_FREEFORM,
113 };
114
115 /* Sets the spots for the current gesture.
116 * The spots are not subject to the inactivity timeout like the pointer
117 * itself it since they are expected to remain visible for so long as
118 * the fingers are on the touch pad.
119 *
120 * The values of the AMOTION_EVENT_AXIS_PRESSURE axis is significant.
121 * For spotCoords, pressure != 0 indicates that the spot's location is being
122 * pressed (not hovering).
123 */
124 virtual void setSpots(SpotGesture spotGesture,
125 const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
126 BitSet32 spotIdBits) = 0;
127
128 /* Removes all spots. */
129 virtual void clearSpots() = 0;
130};
131
132
133/*
134 * Pointer resources.
135 */
136struct PointerResources {
137 SpriteIcon spotHover;
138 SpriteIcon spotTouch;
139 SpriteIcon spotAnchor;
140};
141
142
143/*
144 * Pointer controller policy interface.
145 *
146 * The pointer controller policy is used by the pointer controller to interact with
147 * the Window Manager and other system components.
148 *
149 * The actual implementation is partially supported by callbacks into the DVM
150 * via JNI. This interface is also mocked in the unit tests.
151 */
152class PointerControllerPolicyInterface : public virtual RefBase {
153protected:
154 PointerControllerPolicyInterface() { }
155 virtual ~PointerControllerPolicyInterface() { }
156
157public:
158 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800159};
160
161
162/*
163 * Tracks pointer movements and draws the pointer sprite to a surface.
164 *
165 * Handles pointer acceleration and animation.
166 */
Jeff Brown05dc66a2011-03-02 14:41:58 -0800167class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800168protected:
169 virtual ~PointerController();
170
171public:
Jeff Brown2352b972011-04-12 22:39:53 -0700172 enum InactivityTimeout {
173 INACTIVITY_TIMEOUT_NORMAL = 0,
174 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -0800175 };
176
Jeff Brown2352b972011-04-12 22:39:53 -0700177 PointerController(const sp<PointerControllerPolicyInterface>& policy,
178 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800179
180 virtual bool getBounds(float* outMinX, float* outMinY,
181 float* outMaxX, float* outMaxY) const;
182 virtual void move(float deltaX, float deltaY);
183 virtual void setButtonState(uint32_t buttonState);
184 virtual uint32_t getButtonState() const;
185 virtual void setPosition(float x, float y);
186 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800187 virtual void fade();
188 virtual void unfade();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800189
Jeff Brown2352b972011-04-12 22:39:53 -0700190 virtual void setPresentation(Presentation presentation);
191 virtual void setSpots(SpotGesture spotGesture,
192 const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
193 virtual void clearSpots();
194
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800195 void setDisplaySize(int32_t width, int32_t height);
196 void setDisplayOrientation(int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700197 void setPointerIcon(const SpriteIcon& icon);
198 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800199
200private:
Jeff Brown2352b972011-04-12 22:39:53 -0700201 static const size_t MAX_RECYCLED_SPRITES = 12;
202 static const size_t MAX_SPOTS = 12;
203
Jeff Brown05dc66a2011-03-02 14:41:58 -0800204 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700205 MSG_ANIMATE,
206 MSG_INACTIVITY_TIMEOUT,
207 };
208
209 struct Spot {
210 static const uint32_t INVALID_ID = 0xffffffff;
211
212 uint32_t id;
213 sp<Sprite> sprite;
214 float alpha;
215 float scale;
216 float x, y;
217
218 inline Spot(uint32_t id, const sp<Sprite>& sprite)
219 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
220 x(0.0f), y(0.0f), lastIcon(NULL) { }
221
222 void updateSprite(const SpriteIcon* icon, float x, float y);
223
224 private:
225 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800226 };
227
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800228 mutable Mutex mLock;
229
Jeff Brown2352b972011-04-12 22:39:53 -0700230 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800231 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700232 sp<SpriteController> mSpriteController;
233 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800234
Jeff Brown2352b972011-04-12 22:39:53 -0700235 PointerResources mResources;
236
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800237 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700238 bool animationPending;
239 nsecs_t animationTime;
240
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800241 int32_t displayWidth;
242 int32_t displayHeight;
243 int32_t displayOrientation;
244
Jeff Brown2352b972011-04-12 22:39:53 -0700245 InactivityTimeout inactivityTimeout;
246
247 Presentation presentation;
248 bool presentationChanged;
249
250 bool pointerIsFading;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800251 float pointerX;
252 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700253 float pointerAlpha;
254 sp<Sprite> pointerSprite;
255 SpriteIcon pointerIcon;
256 bool pointerIconChanged;
257
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800258 uint32_t buttonState;
259
Jeff Brown2352b972011-04-12 22:39:53 -0700260 Vector<Spot*> spots;
261 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700262 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800263
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800264 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
265 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800266
267 void handleMessage(const Message& message);
Jeff Brown2352b972011-04-12 22:39:53 -0700268 void doAnimate();
269 void doInactivityTimeout();
270
271 void startAnimationLocked();
272
273 void resetInactivityTimeoutLocked();
274 void sendImmediateInactivityTimeoutLocked();
275 void updatePointerLocked();
276
277 Spot* getSpotLocked(uint32_t id);
278 Spot* createAndAddSpotLocked(uint32_t id);
279 Spot* removeFirstFadingSpotLocked();
280 void releaseSpotLocked(Spot* spot);
281 void fadeOutAndReleaseSpotLocked(Spot* spot);
282 void fadeOutAndReleaseAllSpotsLocked();
283
284 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800285};
286
287} // namespace android
288
289#endif // _UI_POINTER_CONTROLLER_H