blob: e1dab5c7831382f1ea67c0c2c7d8865e897196c1 [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
Jeff Brownb4ff35d2011-01-02 16:37:43 -080034/**
35 * Interface for tracking a single (mouse) pointer.
36 *
37 * The pointer controller is responsible for providing synchronization and for tracking
38 * display orientation changes if needed.
39 */
40class PointerControllerInterface : public virtual RefBase {
41protected:
42 PointerControllerInterface() { }
43 virtual ~PointerControllerInterface() { }
44
45public:
46 /* Gets the bounds of the region that the pointer can traverse.
47 * Returns true if the bounds are available. */
48 virtual bool getBounds(float* outMinX, float* outMinY,
49 float* outMaxX, float* outMaxY) const = 0;
50
51 /* Move the pointer. */
52 virtual void move(float deltaX, float deltaY) = 0;
53
54 /* Sets a mask that indicates which buttons are pressed. */
55 virtual void setButtonState(uint32_t buttonState) = 0;
56
57 /* Gets a mask that indicates which buttons are pressed. */
58 virtual uint32_t getButtonState() const = 0;
59
60 /* Sets the absolute location of the pointer. */
61 virtual void setPosition(float x, float y) = 0;
62
63 /* Gets the absolute location of the pointer. */
64 virtual void getPosition(float* outX, float* outY) const = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -080065
66 /* Fades the pointer out now. */
67 virtual void fade() = 0;
68
69 /* Makes the pointer visible if it has faded out. */
70 virtual void unfade() = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080071};
72
73
74/*
75 * Tracks pointer movements and draws the pointer sprite to a surface.
76 *
77 * Handles pointer acceleration and animation.
78 */
Jeff Brown05dc66a2011-03-02 14:41:58 -080079class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080080protected:
81 virtual ~PointerController();
82
83public:
Jeff Brown05dc66a2011-03-02 14:41:58 -080084 enum InactivityFadeDelay {
85 INACTIVITY_FADE_DELAY_NORMAL = 0,
86 INACTIVITY_FADE_DELAY_SHORT = 1,
87 };
88
89 PointerController(const sp<Looper>& looper, int32_t pointerLayer);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080090
91 virtual bool getBounds(float* outMinX, float* outMinY,
92 float* outMaxX, float* outMaxY) const;
93 virtual void move(float deltaX, float deltaY);
94 virtual void setButtonState(uint32_t buttonState);
95 virtual uint32_t getButtonState() const;
96 virtual void setPosition(float x, float y);
97 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown05dc66a2011-03-02 14:41:58 -080098 virtual void fade();
99 virtual void unfade();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800100
101 void setDisplaySize(int32_t width, int32_t height);
102 void setDisplayOrientation(int32_t orientation);
103 void setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800104 void setInactivityFadeDelay(InactivityFadeDelay inactivityFadeDelay);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800105
106private:
Jeff Brown05dc66a2011-03-02 14:41:58 -0800107 enum {
108 MSG_FADE_STEP = 0,
109 };
110
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800111 mutable Mutex mLock;
112
Jeff Brown05dc66a2011-03-02 14:41:58 -0800113 sp<Looper> mLooper;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800114 int32_t mPointerLayer;
115 sp<SurfaceComposerClient> mSurfaceComposerClient;
116 sp<SurfaceControl> mSurfaceControl;
117
118 struct Locked {
119 int32_t displayWidth;
120 int32_t displayHeight;
121 int32_t displayOrientation;
122
123 float pointerX;
124 float pointerY;
125 uint32_t buttonState;
126
127 SkBitmap* iconBitmap;
128 float iconHotSpotX;
129 float iconHotSpotY;
130
Jeff Brown05dc66a2011-03-02 14:41:58 -0800131 float fadeAlpha;
132 InactivityFadeDelay inactivityFadeDelay;
133
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800134 bool wantVisible;
135 bool visible;
136 bool drawn;
137 } mLocked;
138
Jeff Brown05dc66a2011-03-02 14:41:58 -0800139 sp<WeakMessageHandler> mHandler;
140
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800141 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
142 void setPositionLocked(float x, float y);
143 void updateLocked();
144 bool createSurfaceIfNeededLocked();
145 bool drawPointerIfNeededLocked();
146 bool resizeSurfaceLocked(int32_t width, int32_t height);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800147
148 void handleMessage(const Message& message);
149 bool unfadeBeforeUpdateLocked();
150 void startFadeLocked();
151 void startInactivityFadeDelayLocked();
152 void fadeStepLocked();
153 bool isFadingLocked();
154 nsecs_t getInactivityFadeDelayTimeLocked();
155 void sendFadeStepMessageDelayedLocked(nsecs_t delayTime);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800156};
157
158} // namespace android
159
160#endif // _UI_POINTER_CONTROLLER_H