blob: d467a5a01bcd058fec57e00fd1946dd8da36e128 [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/**
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;
Jeff Brown05dc66a2011-03-02 14:41:58 -080063
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;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080069};
70
71
72/*
73 * Tracks pointer movements and draws the pointer sprite to a surface.
74 *
75 * Handles pointer acceleration and animation.
76 */
Jeff Brown05dc66a2011-03-02 14:41:58 -080077class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080078protected:
79 virtual ~PointerController();
80
81public:
Jeff Brown05dc66a2011-03-02 14:41:58 -080082 enum InactivityFadeDelay {
83 INACTIVITY_FADE_DELAY_NORMAL = 0,
84 INACTIVITY_FADE_DELAY_SHORT = 1,
85 };
86
Jeff Brown5541de92011-04-11 11:54:25 -070087 PointerController(const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080088
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;
Jeff Brown05dc66a2011-03-02 14:41:58 -080096 virtual void fade();
97 virtual void unfade();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080098
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);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800102 void setInactivityFadeDelay(InactivityFadeDelay inactivityFadeDelay);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800103
104private:
Jeff Brown05dc66a2011-03-02 14:41:58 -0800105 enum {
106 MSG_FADE_STEP = 0,
107 };
108
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800109 mutable Mutex mLock;
110
Jeff Brown05dc66a2011-03-02 14:41:58 -0800111 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700112 sp<SpriteController> mSpriteController;
113 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800114
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
Jeff Brown05dc66a2011-03-02 14:41:58 -0800124 float fadeAlpha;
125 InactivityFadeDelay inactivityFadeDelay;
126
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800127 bool visible;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800128
Jeff Brown5541de92011-04-11 11:54:25 -0700129 sp<Sprite> sprite;
130 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800131
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800132 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
133 void setPositionLocked(float x, float y);
134 void updateLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800135
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);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800144};
145
146} // namespace android
147
148#endif // _UI_POINTER_CONTROLLER_H