1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/libui/PointerController.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,266 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#ifndef _UI_POINTER_CONTROLLER_H 1.21 +#define _UI_POINTER_CONTROLLER_H 1.22 + 1.23 +#include "SpriteController.h" 1.24 + 1.25 +#include <ui/DisplayInfo.h> 1.26 +#include "Input.h" 1.27 +#include <utils/BitSet.h> 1.28 +#include <utils/RefBase.h> 1.29 +#include <utils/Looper.h> 1.30 +#include <utils/String8.h> 1.31 + 1.32 +#include <SkBitmap.h> 1.33 + 1.34 +namespace android { 1.35 + 1.36 +/** 1.37 + * Interface for tracking a mouse / touch pad pointer and touch pad spots. 1.38 + * 1.39 + * The spots are sprites on screen that visually represent the positions of 1.40 + * fingers 1.41 + * 1.42 + * The pointer controller is responsible for providing synchronization and for tracking 1.43 + * display orientation changes if needed. 1.44 + */ 1.45 +class PointerControllerInterface : public virtual RefBase { 1.46 +protected: 1.47 + PointerControllerInterface() { } 1.48 + virtual ~PointerControllerInterface() { } 1.49 + 1.50 +public: 1.51 + /* Gets the bounds of the region that the pointer can traverse. 1.52 + * Returns true if the bounds are available. */ 1.53 + virtual bool getBounds(float* outMinX, float* outMinY, 1.54 + float* outMaxX, float* outMaxY) const = 0; 1.55 + 1.56 + /* Move the pointer. */ 1.57 + virtual void move(float deltaX, float deltaY) = 0; 1.58 + 1.59 + /* Sets a mask that indicates which buttons are pressed. */ 1.60 + virtual void setButtonState(int32_t buttonState) = 0; 1.61 + 1.62 + /* Gets a mask that indicates which buttons are pressed. */ 1.63 + virtual int32_t getButtonState() const = 0; 1.64 + 1.65 + /* Sets the absolute location of the pointer. */ 1.66 + virtual void setPosition(float x, float y) = 0; 1.67 + 1.68 + /* Gets the absolute location of the pointer. */ 1.69 + virtual void getPosition(float* outX, float* outY) const = 0; 1.70 + 1.71 + enum Transition { 1.72 + // Fade/unfade immediately. 1.73 + TRANSITION_IMMEDIATE, 1.74 + // Fade/unfade gradually. 1.75 + TRANSITION_GRADUAL, 1.76 + }; 1.77 + 1.78 + /* Fades the pointer out now. */ 1.79 + virtual void fade(Transition transition) = 0; 1.80 + 1.81 + /* Makes the pointer visible if it has faded out. 1.82 + * The pointer never unfades itself automatically. This method must be called 1.83 + * by the client whenever the pointer is moved or a button is pressed and it 1.84 + * wants to ensure that the pointer becomes visible again. */ 1.85 + virtual void unfade(Transition transition) = 0; 1.86 + 1.87 + enum Presentation { 1.88 + // Show the mouse pointer. 1.89 + PRESENTATION_POINTER, 1.90 + // Show spots and a spot anchor in place of the mouse pointer. 1.91 + PRESENTATION_SPOT, 1.92 + }; 1.93 + 1.94 + /* Sets the mode of the pointer controller. */ 1.95 + virtual void setPresentation(Presentation presentation) = 0; 1.96 + 1.97 + /* Sets the spots for the current gesture. 1.98 + * The spots are not subject to the inactivity timeout like the pointer 1.99 + * itself it since they are expected to remain visible for so long as 1.100 + * the fingers are on the touch pad. 1.101 + * 1.102 + * The values of the AMOTION_EVENT_AXIS_PRESSURE axis is significant. 1.103 + * For spotCoords, pressure != 0 indicates that the spot's location is being 1.104 + * pressed (not hovering). 1.105 + */ 1.106 + virtual void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, 1.107 + BitSet32 spotIdBits) = 0; 1.108 + 1.109 + /* Removes all spots. */ 1.110 + virtual void clearSpots() = 0; 1.111 +}; 1.112 + 1.113 + 1.114 +/* 1.115 + * Pointer resources. 1.116 + */ 1.117 +struct PointerResources { 1.118 + SpriteIcon spotHover; 1.119 + SpriteIcon spotTouch; 1.120 + SpriteIcon spotAnchor; 1.121 +}; 1.122 + 1.123 + 1.124 +/* 1.125 + * Pointer controller policy interface. 1.126 + * 1.127 + * The pointer controller policy is used by the pointer controller to interact with 1.128 + * the Window Manager and other system components. 1.129 + * 1.130 + * The actual implementation is partially supported by callbacks into the DVM 1.131 + * via JNI. This interface is also mocked in the unit tests. 1.132 + */ 1.133 +class PointerControllerPolicyInterface : public virtual RefBase { 1.134 +protected: 1.135 + PointerControllerPolicyInterface() { } 1.136 + virtual ~PointerControllerPolicyInterface() { } 1.137 + 1.138 +public: 1.139 + virtual void loadPointerResources(PointerResources* outResources) = 0; 1.140 +}; 1.141 + 1.142 + 1.143 +/* 1.144 + * Tracks pointer movements and draws the pointer sprite to a surface. 1.145 + * 1.146 + * Handles pointer acceleration and animation. 1.147 + */ 1.148 +class PointerController : public PointerControllerInterface, public MessageHandler { 1.149 +protected: 1.150 + virtual ~PointerController(); 1.151 + 1.152 +public: 1.153 + enum InactivityTimeout { 1.154 + INACTIVITY_TIMEOUT_NORMAL = 0, 1.155 + INACTIVITY_TIMEOUT_SHORT = 1, 1.156 + }; 1.157 + 1.158 + PointerController(const sp<PointerControllerPolicyInterface>& policy, 1.159 + const sp<Looper>& looper, const sp<SpriteController>& spriteController); 1.160 + 1.161 + virtual bool getBounds(float* outMinX, float* outMinY, 1.162 + float* outMaxX, float* outMaxY) const; 1.163 + virtual void move(float deltaX, float deltaY); 1.164 + virtual void setButtonState(int32_t buttonState); 1.165 + virtual int32_t getButtonState() const; 1.166 + virtual void setPosition(float x, float y); 1.167 + virtual void getPosition(float* outX, float* outY) const; 1.168 + virtual void fade(Transition transition); 1.169 + virtual void unfade(Transition transition); 1.170 + 1.171 + virtual void setPresentation(Presentation presentation); 1.172 + virtual void setSpots(const PointerCoords* spotCoords, 1.173 + const uint32_t* spotIdToIndex, BitSet32 spotIdBits); 1.174 + virtual void clearSpots(); 1.175 + 1.176 + void setDisplayViewport(int32_t width, int32_t height, int32_t orientation); 1.177 + void setPointerIcon(const SpriteIcon& icon); 1.178 + void setInactivityTimeout(InactivityTimeout inactivityTimeout); 1.179 + 1.180 +private: 1.181 + static const size_t MAX_RECYCLED_SPRITES = 12; 1.182 + static const size_t MAX_SPOTS = 12; 1.183 + 1.184 + enum { 1.185 + MSG_ANIMATE, 1.186 + MSG_INACTIVITY_TIMEOUT, 1.187 + }; 1.188 + 1.189 + struct Spot { 1.190 + static const uint32_t INVALID_ID = 0xffffffff; 1.191 + 1.192 + uint32_t id; 1.193 + sp<Sprite> sprite; 1.194 + float alpha; 1.195 + float scale; 1.196 + float x, y; 1.197 + 1.198 + inline Spot(uint32_t id, const sp<Sprite>& sprite) 1.199 + : id(id), sprite(sprite), alpha(1.0f), scale(1.0f), 1.200 + x(0.0f), y(0.0f), lastIcon(NULL) { } 1.201 + 1.202 + void updateSprite(const SpriteIcon* icon, float x, float y); 1.203 + 1.204 + private: 1.205 + const SpriteIcon* lastIcon; 1.206 + }; 1.207 + 1.208 + mutable Mutex mLock; 1.209 + 1.210 + sp<PointerControllerPolicyInterface> mPolicy; 1.211 + sp<Looper> mLooper; 1.212 + sp<SpriteController> mSpriteController; 1.213 + sp<WeakMessageHandler> mHandler; 1.214 + 1.215 + PointerResources mResources; 1.216 + 1.217 + struct Locked { 1.218 + bool animationPending; 1.219 + nsecs_t animationTime; 1.220 + 1.221 + int32_t displayWidth; 1.222 + int32_t displayHeight; 1.223 + int32_t displayOrientation; 1.224 + 1.225 + InactivityTimeout inactivityTimeout; 1.226 + 1.227 + Presentation presentation; 1.228 + bool presentationChanged; 1.229 + 1.230 + int32_t pointerFadeDirection; 1.231 + float pointerX; 1.232 + float pointerY; 1.233 + float pointerAlpha; 1.234 + sp<Sprite> pointerSprite; 1.235 + SpriteIcon pointerIcon; 1.236 + bool pointerIconChanged; 1.237 + 1.238 + int32_t buttonState; 1.239 + 1.240 + Vector<Spot*> spots; 1.241 + Vector<sp<Sprite> > recycledSprites; 1.242 + } mLocked; 1.243 + 1.244 + bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; 1.245 + void setPositionLocked(float x, float y); 1.246 + 1.247 + void handleMessage(const Message& message); 1.248 + void doAnimate(); 1.249 + void doInactivityTimeout(); 1.250 + 1.251 + void startAnimationLocked(); 1.252 + 1.253 + void resetInactivityTimeoutLocked(); 1.254 + void removeInactivityTimeoutLocked(); 1.255 + void updatePointerLocked(); 1.256 + 1.257 + Spot* getSpotLocked(uint32_t id); 1.258 + Spot* createAndAddSpotLocked(uint32_t id); 1.259 + Spot* removeFirstFadingSpotLocked(); 1.260 + void releaseSpotLocked(Spot* spot); 1.261 + void fadeOutAndReleaseSpotLocked(Spot* spot); 1.262 + void fadeOutAndReleaseAllSpotsLocked(); 1.263 + 1.264 + void loadResources(); 1.265 +}; 1.266 + 1.267 +} // namespace android 1.268 + 1.269 +#endif // _UI_POINTER_CONTROLLER_H