blob: c7435ee304bfcfd15b2a468c58ab2fdb9a6a40ee [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. */
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070056 virtual void setButtonState(int32_t buttonState) = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080057
58 /* Gets a mask that indicates which buttons are pressed. */
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070059 virtual int32_t getButtonState() const = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080060
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,
Jeff Brown79ac9692011-04-19 21:20:10 -070094 // Drag at current location.
95 // Display spot at pressed location.
96 SPOT_GESTURE_DRAG,
Jeff Brown2352b972011-04-12 22:39:53 -070097 // Button pressed but no finger is down.
98 // Display spot at pressed location.
99 SPOT_GESTURE_BUTTON_CLICK,
100 // Button pressed and a finger is down.
101 // Display spot at pressed location.
102 SPOT_GESTURE_BUTTON_DRAG,
103 // One finger down and hovering.
104 // Display spot at the hovered location.
105 SPOT_GESTURE_HOVER,
106 // Two fingers down but not sure in which direction they are moving so we consider
107 // it a press at the pointer location.
108 // Display two spots near the pointer location.
109 SPOT_GESTURE_PRESS,
110 // Two fingers down and moving in same direction.
111 // Display two spots near the pointer location.
112 SPOT_GESTURE_SWIPE,
113 // Two or more fingers down and moving in arbitrary directions.
114 // Display two or more spots near the pointer location, one for each finger.
115 SPOT_GESTURE_FREEFORM,
116 };
117
118 /* Sets the spots for the current gesture.
119 * The spots are not subject to the inactivity timeout like the pointer
120 * itself it since they are expected to remain visible for so long as
121 * the fingers are on the touch pad.
122 *
123 * The values of the AMOTION_EVENT_AXIS_PRESSURE axis is significant.
124 * For spotCoords, pressure != 0 indicates that the spot's location is being
125 * pressed (not hovering).
126 */
127 virtual void setSpots(SpotGesture spotGesture,
128 const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
129 BitSet32 spotIdBits) = 0;
130
131 /* Removes all spots. */
132 virtual void clearSpots() = 0;
133};
134
135
136/*
137 * Pointer resources.
138 */
139struct PointerResources {
140 SpriteIcon spotHover;
141 SpriteIcon spotTouch;
142 SpriteIcon spotAnchor;
143};
144
145
146/*
147 * Pointer controller policy interface.
148 *
149 * The pointer controller policy is used by the pointer controller to interact with
150 * the Window Manager and other system components.
151 *
152 * The actual implementation is partially supported by callbacks into the DVM
153 * via JNI. This interface is also mocked in the unit tests.
154 */
155class PointerControllerPolicyInterface : public virtual RefBase {
156protected:
157 PointerControllerPolicyInterface() { }
158 virtual ~PointerControllerPolicyInterface() { }
159
160public:
161 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800162};
163
164
165/*
166 * Tracks pointer movements and draws the pointer sprite to a surface.
167 *
168 * Handles pointer acceleration and animation.
169 */
Jeff Brown05dc66a2011-03-02 14:41:58 -0800170class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800171protected:
172 virtual ~PointerController();
173
174public:
Jeff Brown2352b972011-04-12 22:39:53 -0700175 enum InactivityTimeout {
176 INACTIVITY_TIMEOUT_NORMAL = 0,
177 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -0800178 };
179
Jeff Brown2352b972011-04-12 22:39:53 -0700180 PointerController(const sp<PointerControllerPolicyInterface>& policy,
181 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800182
183 virtual bool getBounds(float* outMinX, float* outMinY,
184 float* outMaxX, float* outMaxY) const;
185 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 virtual void setButtonState(int32_t buttonState);
187 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800188 virtual void setPosition(float x, float y);
189 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800190 virtual void fade();
191 virtual void unfade();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800192
Jeff Brown2352b972011-04-12 22:39:53 -0700193 virtual void setPresentation(Presentation presentation);
194 virtual void setSpots(SpotGesture spotGesture,
195 const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
196 virtual void clearSpots();
197
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800198 void setDisplaySize(int32_t width, int32_t height);
199 void setDisplayOrientation(int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700200 void setPointerIcon(const SpriteIcon& icon);
201 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800202
203private:
Jeff Brown2352b972011-04-12 22:39:53 -0700204 static const size_t MAX_RECYCLED_SPRITES = 12;
205 static const size_t MAX_SPOTS = 12;
206
Jeff Brown05dc66a2011-03-02 14:41:58 -0800207 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700208 MSG_ANIMATE,
209 MSG_INACTIVITY_TIMEOUT,
210 };
211
212 struct Spot {
213 static const uint32_t INVALID_ID = 0xffffffff;
214
215 uint32_t id;
216 sp<Sprite> sprite;
217 float alpha;
218 float scale;
219 float x, y;
220
221 inline Spot(uint32_t id, const sp<Sprite>& sprite)
222 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
223 x(0.0f), y(0.0f), lastIcon(NULL) { }
224
225 void updateSprite(const SpriteIcon* icon, float x, float y);
226
227 private:
228 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800229 };
230
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800231 mutable Mutex mLock;
232
Jeff Brown2352b972011-04-12 22:39:53 -0700233 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800234 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700235 sp<SpriteController> mSpriteController;
236 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800237
Jeff Brown2352b972011-04-12 22:39:53 -0700238 PointerResources mResources;
239
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800240 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700241 bool animationPending;
242 nsecs_t animationTime;
243
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800244 int32_t displayWidth;
245 int32_t displayHeight;
246 int32_t displayOrientation;
247
Jeff Brown2352b972011-04-12 22:39:53 -0700248 InactivityTimeout inactivityTimeout;
249
250 Presentation presentation;
251 bool presentationChanged;
252
253 bool pointerIsFading;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800254 float pointerX;
255 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700256 float pointerAlpha;
257 sp<Sprite> pointerSprite;
258 SpriteIcon pointerIcon;
259 bool pointerIconChanged;
260
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700261 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800262
Jeff Brown2352b972011-04-12 22:39:53 -0700263 Vector<Spot*> spots;
264 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700265 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800266
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800267 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
268 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800269
270 void handleMessage(const Message& message);
Jeff Brown2352b972011-04-12 22:39:53 -0700271 void doAnimate();
272 void doInactivityTimeout();
273
274 void startAnimationLocked();
275
276 void resetInactivityTimeoutLocked();
277 void sendImmediateInactivityTimeoutLocked();
278 void updatePointerLocked();
279
280 Spot* getSpotLocked(uint32_t id);
281 Spot* createAndAddSpotLocked(uint32_t id);
282 Spot* removeFirstFadingSpotLocked();
283 void releaseSpotLocked(Spot* spot);
284 void fadeOutAndReleaseSpotLocked(Spot* spot);
285 void fadeOutAndReleaseAllSpotsLocked();
286
287 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800288};
289
290} // namespace android
291
292#endif // _UI_POINTER_CONTROLLER_H