blob: 9879ec4aa539a1b246b66a1f8523120ae0cc81bb [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 Browna6dbfdd2011-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 Brown86ea1f52011-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
Jeff Brown538881e2011-05-25 18:23:38 -070067 enum Transition {
68 // Fade/unfade immediately.
69 TRANSITION_IMMEDIATE,
70 // Fade/unfade gradually.
71 TRANSITION_GRADUAL,
72 };
73
Jeff Brown05dc66a2011-03-02 14:41:58 -080074 /* Fades the pointer out now. */
Jeff Brown538881e2011-05-25 18:23:38 -070075 virtual void fade(Transition transition) = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -080076
Jeff Brown86ea1f52011-04-12 22:39:53 -070077 /* Makes the pointer visible if it has faded out.
78 * The pointer never unfades itself automatically. This method must be called
79 * by the client whenever the pointer is moved or a button is pressed and it
80 * wants to ensure that the pointer becomes visible again. */
Jeff Brown538881e2011-05-25 18:23:38 -070081 virtual void unfade(Transition transition) = 0;
Jeff Brown86ea1f52011-04-12 22:39:53 -070082
83 enum Presentation {
84 // Show the mouse pointer.
85 PRESENTATION_POINTER,
86 // Show spots and a spot anchor in place of the mouse pointer.
87 PRESENTATION_SPOT,
88 };
89
90 /* Sets the mode of the pointer controller. */
91 virtual void setPresentation(Presentation presentation) = 0;
92
Jeff Brown86ea1f52011-04-12 22:39:53 -070093 /* Sets the spots for the current gesture.
94 * The spots are not subject to the inactivity timeout like the pointer
95 * itself it since they are expected to remain visible for so long as
96 * the fingers are on the touch pad.
97 *
98 * The values of the AMOTION_EVENT_AXIS_PRESSURE axis is significant.
99 * For spotCoords, pressure != 0 indicates that the spot's location is being
100 * pressed (not hovering).
101 */
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700102 virtual void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
Jeff Brown86ea1f52011-04-12 22:39:53 -0700103 BitSet32 spotIdBits) = 0;
104
105 /* Removes all spots. */
106 virtual void clearSpots() = 0;
107};
108
109
110/*
111 * Pointer resources.
112 */
113struct PointerResources {
114 SpriteIcon spotHover;
115 SpriteIcon spotTouch;
116 SpriteIcon spotAnchor;
117};
118
119
120/*
121 * Pointer controller policy interface.
122 *
123 * The pointer controller policy is used by the pointer controller to interact with
124 * the Window Manager and other system components.
125 *
126 * The actual implementation is partially supported by callbacks into the DVM
127 * via JNI. This interface is also mocked in the unit tests.
128 */
129class PointerControllerPolicyInterface : public virtual RefBase {
130protected:
131 PointerControllerPolicyInterface() { }
132 virtual ~PointerControllerPolicyInterface() { }
133
134public:
135 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800136};
137
138
139/*
140 * Tracks pointer movements and draws the pointer sprite to a surface.
141 *
142 * Handles pointer acceleration and animation.
143 */
Jeff Brown05dc66a2011-03-02 14:41:58 -0800144class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145protected:
146 virtual ~PointerController();
147
148public:
Jeff Brown86ea1f52011-04-12 22:39:53 -0700149 enum InactivityTimeout {
150 INACTIVITY_TIMEOUT_NORMAL = 0,
151 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -0800152 };
153
Jeff Brown86ea1f52011-04-12 22:39:53 -0700154 PointerController(const sp<PointerControllerPolicyInterface>& policy,
155 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800156
157 virtual bool getBounds(float* outMinX, float* outMinY,
158 float* outMaxX, float* outMaxY) const;
159 virtual void move(float deltaX, float deltaY);
160 virtual void setButtonState(uint32_t buttonState);
161 virtual uint32_t getButtonState() const;
162 virtual void setPosition(float x, float y);
163 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -0700164 virtual void fade(Transition transition);
165 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800166
Jeff Brown86ea1f52011-04-12 22:39:53 -0700167 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700168 virtual void setSpots(const PointerCoords* spotCoords,
169 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown86ea1f52011-04-12 22:39:53 -0700170 virtual void clearSpots();
171
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800172 void setDisplaySize(int32_t width, int32_t height);
173 void setDisplayOrientation(int32_t orientation);
Jeff Brown86ea1f52011-04-12 22:39:53 -0700174 void setPointerIcon(const SpriteIcon& icon);
175 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800176
177private:
Jeff Brown86ea1f52011-04-12 22:39:53 -0700178 static const size_t MAX_RECYCLED_SPRITES = 12;
179 static const size_t MAX_SPOTS = 12;
180
Jeff Brown05dc66a2011-03-02 14:41:58 -0800181 enum {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700182 MSG_ANIMATE,
183 MSG_INACTIVITY_TIMEOUT,
184 };
185
186 struct Spot {
187 static const uint32_t INVALID_ID = 0xffffffff;
188
189 uint32_t id;
190 sp<Sprite> sprite;
191 float alpha;
192 float scale;
193 float x, y;
194
195 inline Spot(uint32_t id, const sp<Sprite>& sprite)
196 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
197 x(0.0f), y(0.0f), lastIcon(NULL) { }
198
199 void updateSprite(const SpriteIcon* icon, float x, float y);
200
201 private:
202 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800203 };
204
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800205 mutable Mutex mLock;
206
Jeff Brown86ea1f52011-04-12 22:39:53 -0700207 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800208 sp<Looper> mLooper;
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700209 sp<SpriteController> mSpriteController;
210 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800211
Jeff Brown86ea1f52011-04-12 22:39:53 -0700212 PointerResources mResources;
213
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800214 struct Locked {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700215 bool animationPending;
216 nsecs_t animationTime;
217
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800218 int32_t displayWidth;
219 int32_t displayHeight;
220 int32_t displayOrientation;
221
Jeff Brown86ea1f52011-04-12 22:39:53 -0700222 InactivityTimeout inactivityTimeout;
223
224 Presentation presentation;
225 bool presentationChanged;
226
Jeff Brown538881e2011-05-25 18:23:38 -0700227 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800228 float pointerX;
229 float pointerY;
Jeff Brown86ea1f52011-04-12 22:39:53 -0700230 float pointerAlpha;
231 sp<Sprite> pointerSprite;
232 SpriteIcon pointerIcon;
233 bool pointerIconChanged;
234
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800235 uint32_t buttonState;
236
Jeff Brown86ea1f52011-04-12 22:39:53 -0700237 Vector<Spot*> spots;
238 Vector<sp<Sprite> > recycledSprites;
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700239 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800240
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800241 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
242 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800243
244 void handleMessage(const Message& message);
Jeff Brown86ea1f52011-04-12 22:39:53 -0700245 void doAnimate();
246 void doInactivityTimeout();
247
248 void startAnimationLocked();
249
250 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700251 void removeInactivityTimeoutLocked();
Jeff Brown86ea1f52011-04-12 22:39:53 -0700252 void updatePointerLocked();
253
254 Spot* getSpotLocked(uint32_t id);
255 Spot* createAndAddSpotLocked(uint32_t id);
256 Spot* removeFirstFadingSpotLocked();
257 void releaseSpotLocked(Spot* spot);
258 void fadeOutAndReleaseSpotLocked(Spot* spot);
259 void fadeOutAndReleaseAllSpotsLocked();
260
261 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800262};
263
264} // namespace android
265
266#endif // _UI_POINTER_CONTROLLER_H