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 <input/Input.h>
24#include <inputflinger/PointerControllerInterface.h>
25#include <utils/BitSet.h>
26#include <utils/RefBase.h>
27#include <utils/Looper.h>
28#include <utils/String8.h>
29
30#include <SkBitmap.h>
31
32namespace android {
33
34/*
35 * Pointer resources.
36 */
37struct PointerResources {
38    SpriteIcon spotHover;
39    SpriteIcon spotTouch;
40    SpriteIcon spotAnchor;
41};
42
43
44/*
45 * Pointer controller policy interface.
46 *
47 * The pointer controller policy is used by the pointer controller to interact with
48 * the Window Manager and other system components.
49 *
50 * The actual implementation is partially supported by callbacks into the DVM
51 * via JNI.  This interface is also mocked in the unit tests.
52 */
53class PointerControllerPolicyInterface : public virtual RefBase {
54protected:
55    PointerControllerPolicyInterface() { }
56    virtual ~PointerControllerPolicyInterface() { }
57
58public:
59    virtual void loadPointerResources(PointerResources* outResources) = 0;
60};
61
62
63/*
64 * Tracks pointer movements and draws the pointer sprite to a surface.
65 *
66 * Handles pointer acceleration and animation.
67 */
68class PointerController : public PointerControllerInterface, public MessageHandler {
69protected:
70    virtual ~PointerController();
71
72public:
73    enum InactivityTimeout {
74        INACTIVITY_TIMEOUT_NORMAL = 0,
75        INACTIVITY_TIMEOUT_SHORT = 1,
76    };
77
78    PointerController(const sp<PointerControllerPolicyInterface>& policy,
79            const sp<Looper>& looper, const sp<SpriteController>& spriteController);
80
81    virtual bool getBounds(float* outMinX, float* outMinY,
82            float* outMaxX, float* outMaxY) const;
83    virtual void move(float deltaX, float deltaY);
84    virtual void setButtonState(int32_t buttonState);
85    virtual int32_t getButtonState() const;
86    virtual void setPosition(float x, float y);
87    virtual void getPosition(float* outX, float* outY) const;
88    virtual void fade(Transition transition);
89    virtual void unfade(Transition transition);
90
91    virtual void setPresentation(Presentation presentation);
92    virtual void setSpots(const PointerCoords* spotCoords,
93            const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
94    virtual void clearSpots();
95
96    void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
97    void setPointerIcon(const SpriteIcon& icon);
98    void setInactivityTimeout(InactivityTimeout inactivityTimeout);
99
100private:
101    static const size_t MAX_RECYCLED_SPRITES = 12;
102    static const size_t MAX_SPOTS = 12;
103
104    enum {
105        MSG_ANIMATE,
106        MSG_INACTIVITY_TIMEOUT,
107    };
108
109    struct Spot {
110        static const uint32_t INVALID_ID = 0xffffffff;
111
112        uint32_t id;
113        sp<Sprite> sprite;
114        float alpha;
115        float scale;
116        float x, y;
117
118        inline Spot(uint32_t id, const sp<Sprite>& sprite)
119                : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
120                  x(0.0f), y(0.0f), lastIcon(NULL) { }
121
122        void updateSprite(const SpriteIcon* icon, float x, float y);
123
124    private:
125        const SpriteIcon* lastIcon;
126    };
127
128    mutable Mutex mLock;
129
130    sp<PointerControllerPolicyInterface> mPolicy;
131    sp<Looper> mLooper;
132    sp<SpriteController> mSpriteController;
133    sp<WeakMessageHandler> mHandler;
134
135    PointerResources mResources;
136
137    struct Locked {
138        bool animationPending;
139        nsecs_t animationTime;
140
141        int32_t displayWidth;
142        int32_t displayHeight;
143        int32_t displayOrientation;
144
145        InactivityTimeout inactivityTimeout;
146
147        Presentation presentation;
148        bool presentationChanged;
149
150        int32_t pointerFadeDirection;
151        float pointerX;
152        float pointerY;
153        float pointerAlpha;
154        sp<Sprite> pointerSprite;
155        SpriteIcon pointerIcon;
156        bool pointerIconChanged;
157
158        int32_t buttonState;
159
160        Vector<Spot*> spots;
161        Vector<sp<Sprite> > recycledSprites;
162    } mLocked;
163
164    bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
165    void setPositionLocked(float x, float y);
166
167    void handleMessage(const Message& message);
168    void doAnimate();
169    void doInactivityTimeout();
170
171    void startAnimationLocked();
172
173    void resetInactivityTimeoutLocked();
174    void removeInactivityTimeoutLocked();
175    void updatePointerLocked();
176
177    Spot* getSpotLocked(uint32_t id);
178    Spot* createAndAddSpotLocked(uint32_t id);
179    Spot* removeFirstFadingSpotLocked();
180    void releaseSpotLocked(Spot* spot);
181    void fadeOutAndReleaseSpotLocked(Spot* spot);
182    void fadeOutAndReleaseAllSpotsLocked();
183
184    void loadResources();
185};
186
187} // namespace android
188
189#endif // _UI_POINTER_CONTROLLER_H
190