PointerController.h revision 5541de9ea3513a12d1ac2ad07e7e04a3aa7741a0
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 20#include "SpriteController.h" 21 22#include <ui/DisplayInfo.h> 23#include <ui/Input.h> 24#include <utils/RefBase.h> 25#include <utils/Looper.h> 26#include <utils/String8.h> 27 28#include <SkBitmap.h> 29 30namespace android { 31 32/** 33 * Interface for tracking a single (mouse) pointer. 34 * 35 * The pointer controller is responsible for providing synchronization and for tracking 36 * display orientation changes if needed. 37 */ 38class PointerControllerInterface : public virtual RefBase { 39protected: 40 PointerControllerInterface() { } 41 virtual ~PointerControllerInterface() { } 42 43public: 44 /* Gets the bounds of the region that the pointer can traverse. 45 * Returns true if the bounds are available. */ 46 virtual bool getBounds(float* outMinX, float* outMinY, 47 float* outMaxX, float* outMaxY) const = 0; 48 49 /* Move the pointer. */ 50 virtual void move(float deltaX, float deltaY) = 0; 51 52 /* Sets a mask that indicates which buttons are pressed. */ 53 virtual void setButtonState(uint32_t buttonState) = 0; 54 55 /* Gets a mask that indicates which buttons are pressed. */ 56 virtual uint32_t getButtonState() const = 0; 57 58 /* Sets the absolute location of the pointer. */ 59 virtual void setPosition(float x, float y) = 0; 60 61 /* Gets the absolute location of the pointer. */ 62 virtual void getPosition(float* outX, float* outY) const = 0; 63 64 /* Fades the pointer out now. */ 65 virtual void fade() = 0; 66 67 /* Makes the pointer visible if it has faded out. */ 68 virtual void unfade() = 0; 69}; 70 71 72/* 73 * Tracks pointer movements and draws the pointer sprite to a surface. 74 * 75 * Handles pointer acceleration and animation. 76 */ 77class PointerController : public PointerControllerInterface, public MessageHandler { 78protected: 79 virtual ~PointerController(); 80 81public: 82 enum InactivityFadeDelay { 83 INACTIVITY_FADE_DELAY_NORMAL = 0, 84 INACTIVITY_FADE_DELAY_SHORT = 1, 85 }; 86 87 PointerController(const sp<Looper>& looper, const sp<SpriteController>& spriteController); 88 89 virtual bool getBounds(float* outMinX, float* outMinY, 90 float* outMaxX, float* outMaxY) const; 91 virtual void move(float deltaX, float deltaY); 92 virtual void setButtonState(uint32_t buttonState); 93 virtual uint32_t getButtonState() const; 94 virtual void setPosition(float x, float y); 95 virtual void getPosition(float* outX, float* outY) const; 96 virtual void fade(); 97 virtual void unfade(); 98 99 void setDisplaySize(int32_t width, int32_t height); 100 void setDisplayOrientation(int32_t orientation); 101 void setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY); 102 void setInactivityFadeDelay(InactivityFadeDelay inactivityFadeDelay); 103 104private: 105 enum { 106 MSG_FADE_STEP = 0, 107 }; 108 109 mutable Mutex mLock; 110 111 sp<Looper> mLooper; 112 sp<SpriteController> mSpriteController; 113 sp<WeakMessageHandler> mHandler; 114 115 struct Locked { 116 int32_t displayWidth; 117 int32_t displayHeight; 118 int32_t displayOrientation; 119 120 float pointerX; 121 float pointerY; 122 uint32_t buttonState; 123 124 float fadeAlpha; 125 InactivityFadeDelay inactivityFadeDelay; 126 127 bool visible; 128 129 sp<Sprite> sprite; 130 } mLocked; 131 132 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; 133 void setPositionLocked(float x, float y); 134 void updateLocked(); 135 136 void handleMessage(const Message& message); 137 bool unfadeBeforeUpdateLocked(); 138 void startFadeLocked(); 139 void startInactivityFadeDelayLocked(); 140 void fadeStepLocked(); 141 bool isFadingLocked(); 142 nsecs_t getInactivityFadeDelayTimeLocked(); 143 void sendFadeStepMessageDelayedLocked(nsecs_t delayTime); 144}; 145 146} // namespace android 147 148#endif // _UI_POINTER_CONTROLLER_H 149