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