Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 20 | #include "SpriteController.h" |
| 21 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 22 | #include <ui/DisplayInfo.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 23 | #include <ui/Input.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 25 | #include <utils/Looper.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 26 | #include <utils/String8.h> |
| 27 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 28 | #include <SkBitmap.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 32 | /** |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 33 | * 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 Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 37 | * |
| 38 | * The pointer controller is responsible for providing synchronization and for tracking |
| 39 | * display orientation changes if needed. |
| 40 | */ |
| 41 | class PointerControllerInterface : public virtual RefBase { |
| 42 | protected: |
| 43 | PointerControllerInterface() { } |
| 44 | virtual ~PointerControllerInterface() { } |
| 45 | |
| 46 | public: |
| 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 Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 66 | |
| 67 | /* Fades the pointer out now. */ |
| 68 | virtual void fade() = 0; |
| 69 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 70 | /* 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 Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 74 | virtual void unfade() = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 75 | |
| 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 Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 94 | // Drag at current location. |
| 95 | // Display spot at pressed location. |
| 96 | SPOT_GESTURE_DRAG, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 97 | // 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 | */ |
| 139 | struct 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 | */ |
| 155 | class PointerControllerPolicyInterface : public virtual RefBase { |
| 156 | protected: |
| 157 | PointerControllerPolicyInterface() { } |
| 158 | virtual ~PointerControllerPolicyInterface() { } |
| 159 | |
| 160 | public: |
| 161 | virtual void loadPointerResources(PointerResources* outResources) = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * Tracks pointer movements and draws the pointer sprite to a surface. |
| 167 | * |
| 168 | * Handles pointer acceleration and animation. |
| 169 | */ |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 170 | class PointerController : public PointerControllerInterface, public MessageHandler { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 171 | protected: |
| 172 | virtual ~PointerController(); |
| 173 | |
| 174 | public: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 175 | enum InactivityTimeout { |
| 176 | INACTIVITY_TIMEOUT_NORMAL = 0, |
| 177 | INACTIVITY_TIMEOUT_SHORT = 1, |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 180 | PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 181 | const sp<Looper>& looper, const sp<SpriteController>& spriteController); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 182 | |
| 183 | virtual bool getBounds(float* outMinX, float* outMinY, |
| 184 | float* outMaxX, float* outMaxY) const; |
| 185 | virtual void move(float deltaX, float deltaY); |
| 186 | virtual void setButtonState(uint32_t buttonState); |
| 187 | virtual uint32_t getButtonState() const; |
| 188 | virtual void setPosition(float x, float y); |
| 189 | virtual void getPosition(float* outX, float* outY) const; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 190 | virtual void fade(); |
| 191 | virtual void unfade(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 192 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 193 | 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 Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 198 | void setDisplaySize(int32_t width, int32_t height); |
| 199 | void setDisplayOrientation(int32_t orientation); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 200 | void setPointerIcon(const SpriteIcon& icon); |
| 201 | void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 202 | |
| 203 | private: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 204 | static const size_t MAX_RECYCLED_SPRITES = 12; |
| 205 | static const size_t MAX_SPOTS = 12; |
| 206 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 207 | enum { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 208 | 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 Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 231 | mutable Mutex mLock; |
| 232 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 233 | sp<PointerControllerPolicyInterface> mPolicy; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 234 | sp<Looper> mLooper; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 235 | sp<SpriteController> mSpriteController; |
| 236 | sp<WeakMessageHandler> mHandler; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 237 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 238 | PointerResources mResources; |
| 239 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 240 | struct Locked { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 241 | bool animationPending; |
| 242 | nsecs_t animationTime; |
| 243 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 244 | int32_t displayWidth; |
| 245 | int32_t displayHeight; |
| 246 | int32_t displayOrientation; |
| 247 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 248 | InactivityTimeout inactivityTimeout; |
| 249 | |
| 250 | Presentation presentation; |
| 251 | bool presentationChanged; |
| 252 | |
| 253 | bool pointerIsFading; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 254 | float pointerX; |
| 255 | float pointerY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 256 | float pointerAlpha; |
| 257 | sp<Sprite> pointerSprite; |
| 258 | SpriteIcon pointerIcon; |
| 259 | bool pointerIconChanged; |
| 260 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 261 | uint32_t buttonState; |
| 262 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 263 | Vector<Spot*> spots; |
| 264 | Vector<sp<Sprite> > recycledSprites; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 265 | } mLocked; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 266 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 267 | bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; |
| 268 | void setPositionLocked(float x, float y); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 269 | |
| 270 | void handleMessage(const Message& message); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 271 | 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 Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | } // namespace android |
| 291 | |
| 292 | #endif // _UI_POINTER_CONTROLLER_H |