blob: e28dd7da0e4ac82dfbe6b5dfe048e4445495d348 [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
20#include <ui/DisplayInfo.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080021#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080022#include <utils/RefBase.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080023#include <utils/Looper.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080024#include <utils/String8.h>
25
26#include <surfaceflinger/Surface.h>
27#include <surfaceflinger/SurfaceComposerClient.h>
28#include <surfaceflinger/ISurfaceComposer.h>
29
30#include <SkBitmap.h>
31
32namespace android {
33
34enum {
35 POINTER_BUTTON_1 = 1 << 0,
36};
37
38/**
39 * Interface for tracking a single (mouse) pointer.
40 *
41 * The pointer controller is responsible for providing synchronization and for tracking
42 * display orientation changes if needed.
43 */
44class PointerControllerInterface : public virtual RefBase {
45protected:
46 PointerControllerInterface() { }
47 virtual ~PointerControllerInterface() { }
48
49public:
50 /* Gets the bounds of the region that the pointer can traverse.
51 * Returns true if the bounds are available. */
52 virtual bool getBounds(float* outMinX, float* outMinY,
53 float* outMaxX, float* outMaxY) const = 0;
54
55 /* Move the pointer. */
56 virtual void move(float deltaX, float deltaY) = 0;
57
58 /* Sets a mask that indicates which buttons are pressed. */
59 virtual void setButtonState(uint32_t buttonState) = 0;
60
61 /* Gets a mask that indicates which buttons are pressed. */
62 virtual uint32_t getButtonState() const = 0;
63
64 /* Sets the absolute location of the pointer. */
65 virtual void setPosition(float x, float y) = 0;
66
67 /* Gets the absolute location of the pointer. */
68 virtual void getPosition(float* outX, float* outY) const = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -080069
70 /* Fades the pointer out now. */
71 virtual void fade() = 0;
72
73 /* Makes the pointer visible if it has faded out. */
74 virtual void unfade() = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080075};
76
77
78/*
79 * Tracks pointer movements and draws the pointer sprite to a surface.
80 *
81 * Handles pointer acceleration and animation.
82 */
Jeff Brown05dc66a2011-03-02 14:41:58 -080083class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080084protected:
85 virtual ~PointerController();
86
87public:
Jeff Brown05dc66a2011-03-02 14:41:58 -080088 enum InactivityFadeDelay {
89 INACTIVITY_FADE_DELAY_NORMAL = 0,
90 INACTIVITY_FADE_DELAY_SHORT = 1,
91 };
92
93 PointerController(const sp<Looper>& looper, int32_t pointerLayer);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080094
95 virtual bool getBounds(float* outMinX, float* outMinY,
96 float* outMaxX, float* outMaxY) const;
97 virtual void move(float deltaX, float deltaY);
98 virtual void setButtonState(uint32_t buttonState);
99 virtual uint32_t getButtonState() const;
100 virtual void setPosition(float x, float y);
101 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800102 virtual void fade();
103 virtual void unfade();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800104
105 void setDisplaySize(int32_t width, int32_t height);
106 void setDisplayOrientation(int32_t orientation);
107 void setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800108 void setInactivityFadeDelay(InactivityFadeDelay inactivityFadeDelay);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800109
110private:
Jeff Brown05dc66a2011-03-02 14:41:58 -0800111 enum {
112 MSG_FADE_STEP = 0,
113 };
114
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800115 mutable Mutex mLock;
116
Jeff Brown05dc66a2011-03-02 14:41:58 -0800117 sp<Looper> mLooper;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800118 int32_t mPointerLayer;
119 sp<SurfaceComposerClient> mSurfaceComposerClient;
120 sp<SurfaceControl> mSurfaceControl;
121
122 struct Locked {
123 int32_t displayWidth;
124 int32_t displayHeight;
125 int32_t displayOrientation;
126
127 float pointerX;
128 float pointerY;
129 uint32_t buttonState;
130
131 SkBitmap* iconBitmap;
132 float iconHotSpotX;
133 float iconHotSpotY;
134
Jeff Brown05dc66a2011-03-02 14:41:58 -0800135 float fadeAlpha;
136 InactivityFadeDelay inactivityFadeDelay;
137
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800138 bool wantVisible;
139 bool visible;
140 bool drawn;
141 } mLocked;
142
Jeff Brown05dc66a2011-03-02 14:41:58 -0800143 sp<WeakMessageHandler> mHandler;
144
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
146 void setPositionLocked(float x, float y);
147 void updateLocked();
148 bool createSurfaceIfNeededLocked();
149 bool drawPointerIfNeededLocked();
150 bool resizeSurfaceLocked(int32_t width, int32_t height);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800151
152 void handleMessage(const Message& message);
153 bool unfadeBeforeUpdateLocked();
154 void startFadeLocked();
155 void startInactivityFadeDelayLocked();
156 void fadeStepLocked();
157 bool isFadingLocked();
158 nsecs_t getInactivityFadeDelayTimeLocked();
159 void sendFadeStepMessageDelayedLocked(nsecs_t delayTime);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800160};
161
162} // namespace android
163
164#endif // _UI_POINTER_CONTROLLER_H