blob: c1381f32a37f82ba6b761c2cbe7b38f74c0df71f [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
Jun Mukai1db53972015-09-11 18:08:31 -070022#include <map>
Jun Mukai808196f2015-10-28 16:46:44 -070023#include <vector>
Jun Mukai1db53972015-09-11 18:08:31 -070024
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070026#include <input/Input.h>
Michael Wrightd6b473712014-02-10 15:56:36 -080027#include <inputflinger/PointerControllerInterface.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070028#include <utils/BitSet.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080029#include <utils/RefBase.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080030#include <utils/Looper.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080031#include <utils/String8.h>
Jun Mukaic0c0ac32015-10-27 10:09:21 -070032#include <gui/DisplayEventReceiver.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033
Jeff Brownb4ff35d2011-01-02 16:37:43 -080034namespace android {
35
Jeff Brown2352b972011-04-12 22:39:53 -070036/*
37 * Pointer resources.
38 */
39struct PointerResources {
40 SpriteIcon spotHover;
41 SpriteIcon spotTouch;
42 SpriteIcon spotAnchor;
43};
44
Jun Mukai808196f2015-10-28 16:46:44 -070045struct PointerAnimation {
46 std::vector<SpriteIcon> animationFrames;
47 nsecs_t durationPerFrame;
48};
49
Jeff Brown2352b972011-04-12 22:39:53 -070050/*
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 */
59class PointerControllerPolicyInterface : public virtual RefBase {
60protected:
61 PointerControllerPolicyInterface() { }
62 virtual ~PointerControllerPolicyInterface() { }
63
64public:
Jun Mukai19a56012015-11-24 11:25:52 -080065 virtual void loadPointerIcon(SpriteIcon* icon) = 0;
Jeff Brown2352b972011-04-12 22:39:53 -070066 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jun Mukai808196f2015-10-28 16:46:44 -070067 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
68 std::map<int32_t, PointerAnimation>* outAnimationResources) = 0;
Jun Mukai5ec74202015-10-07 16:58:09 +090069 virtual int32_t getDefaultPointerIconId() = 0;
Jun Mukaid4eaef72015-10-30 15:54:33 -070070 virtual int32_t getCustomPointerIconId() = 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 */
Jun Mukaic0c0ac32015-10-27 10:09:21 -070079class PointerController : public PointerControllerInterface, public MessageHandler,
80 public LooperCallback {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080081protected:
82 virtual ~PointerController();
83
84public:
Jeff Brown2352b972011-04-12 22:39:53 -070085 enum InactivityTimeout {
86 INACTIVITY_TIMEOUT_NORMAL = 0,
87 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080088 };
89
Jeff Brown2352b972011-04-12 22:39:53 -070090 PointerController(const sp<PointerControllerPolicyInterface>& policy,
91 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080092
93 virtual bool getBounds(float* outMinX, float* outMinY,
94 float* outMaxX, float* outMaxY) const;
95 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070096 virtual void setButtonState(int32_t buttonState);
97 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080098 virtual void setPosition(float x, float y);
99 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -0700100 virtual void fade(Transition transition);
101 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800102
Jeff Brown2352b972011-04-12 22:39:53 -0700103 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -0700104 virtual void setSpots(const PointerCoords* spotCoords,
105 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -0700106 virtual void clearSpots();
107
Jun Mukai808196f2015-10-28 16:46:44 -0700108 void updatePointerShape(int32_t iconId);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700109 void setCustomPointerIcon(const SpriteIcon& icon);
Jeff Brownd728bf52012-09-08 18:05:28 -0700110 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700111 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jun Mukai19a56012015-11-24 11:25:52 -0800112 void reloadPointerResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800113
Jun Mukai347e5d42015-12-03 01:13:31 -0800114 /* Detach or attach the pointer icon status. When detached, the pointer icon disappears
115 * and the icon location does not change at all. */
116 void detachPointerIcon(bool detached);
117
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800118private:
Jeff Brown2352b972011-04-12 22:39:53 -0700119 static const size_t MAX_RECYCLED_SPRITES = 12;
120 static const size_t MAX_SPOTS = 12;
121
Jeff Brown05dc66a2011-03-02 14:41:58 -0800122 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700123 MSG_INACTIVITY_TIMEOUT,
124 };
125
126 struct Spot {
127 static const uint32_t INVALID_ID = 0xffffffff;
128
129 uint32_t id;
130 sp<Sprite> sprite;
131 float alpha;
132 float scale;
133 float x, y;
134
135 inline Spot(uint32_t id, const sp<Sprite>& sprite)
136 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
137 x(0.0f), y(0.0f), lastIcon(NULL) { }
138
139 void updateSprite(const SpriteIcon* icon, float x, float y);
140
141 private:
142 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800143 };
144
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145 mutable Mutex mLock;
146
Jeff Brown2352b972011-04-12 22:39:53 -0700147 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800148 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700149 sp<SpriteController> mSpriteController;
150 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800151
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700152 DisplayEventReceiver mDisplayEventReceiver;
153
Jeff Brown2352b972011-04-12 22:39:53 -0700154 PointerResources mResources;
155
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800156 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700157 bool animationPending;
158 nsecs_t animationTime;
159
Jun Mukai808196f2015-10-28 16:46:44 -0700160 size_t animationFrameIndex;
161 nsecs_t lastFrameUpdatedTime;
162
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800163 int32_t displayWidth;
164 int32_t displayHeight;
165 int32_t displayOrientation;
166
Jeff Brown2352b972011-04-12 22:39:53 -0700167 InactivityTimeout inactivityTimeout;
168
169 Presentation presentation;
170 bool presentationChanged;
171
Jeff Brown538881e2011-05-25 18:23:38 -0700172 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800173 float pointerX;
174 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700175 float pointerAlpha;
176 sp<Sprite> pointerSprite;
177 SpriteIcon pointerIcon;
178 bool pointerIconChanged;
179
Jun Mukai808196f2015-10-28 16:46:44 -0700180 std::map<int32_t, SpriteIcon> additionalMouseResources;
181 std::map<int32_t, PointerAnimation> animationResources;
Jun Mukai1db53972015-09-11 18:08:31 -0700182
183 int32_t requestedPointerShape;
184
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700185 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800186
Jun Mukai347e5d42015-12-03 01:13:31 -0800187 bool iconDetached;
188
Jeff Brown2352b972011-04-12 22:39:53 -0700189 Vector<Spot*> spots;
190 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700191 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800192
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800193 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
194 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800195
196 void handleMessage(const Message& message);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700197 int handleEvent(int fd, int events, void* data);
198 void doAnimate(nsecs_t timestamp);
Jun Mukai808196f2015-10-28 16:46:44 -0700199 bool doFadingAnimationLocked(nsecs_t timestamp);
200 bool doBitmapAnimationLocked(nsecs_t timestamp);
Jeff Brown2352b972011-04-12 22:39:53 -0700201 void doInactivityTimeout();
202
203 void startAnimationLocked();
204
205 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700206 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700207 void updatePointerLocked();
208
209 Spot* getSpotLocked(uint32_t id);
210 Spot* createAndAddSpotLocked(uint32_t id);
211 Spot* removeFirstFadingSpotLocked();
212 void releaseSpotLocked(Spot* spot);
213 void fadeOutAndReleaseSpotLocked(Spot* spot);
214 void fadeOutAndReleaseAllSpotsLocked();
215
216 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800217};
218
219} // namespace android
220
221#endif // _UI_POINTER_CONTROLLER_H