blob: 24a16819384e408ac80111ebc6b76f6c4b2d9689 [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>
23
Jeff Brownb4ff35d2011-01-02 16:37:43 -080024#include <ui/DisplayInfo.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070025#include <input/Input.h>
Michael Wrightd6b473712014-02-10 15:56:36 -080026#include <inputflinger/PointerControllerInterface.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070027#include <utils/BitSet.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080028#include <utils/RefBase.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080029#include <utils/Looper.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080030#include <utils/String8.h>
31
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032#include <SkBitmap.h>
33
34namespace 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
Jeff Brown2352b972011-04-12 22:39:53 -070045/*
46 * Pointer controller policy interface.
47 *
48 * The pointer controller policy is used by the pointer controller to interact with
49 * the Window Manager and other system components.
50 *
51 * The actual implementation is partially supported by callbacks into the DVM
52 * via JNI. This interface is also mocked in the unit tests.
53 */
54class PointerControllerPolicyInterface : public virtual RefBase {
55protected:
56 PointerControllerPolicyInterface() { }
57 virtual ~PointerControllerPolicyInterface() { }
58
59public:
60 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jun Mukai5ec74202015-10-07 16:58:09 +090061 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources) = 0;
62 virtual int32_t getDefaultPointerIconId() = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080063};
64
65
66/*
67 * Tracks pointer movements and draws the pointer sprite to a surface.
68 *
69 * Handles pointer acceleration and animation.
70 */
Jeff Brown05dc66a2011-03-02 14:41:58 -080071class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080072protected:
73 virtual ~PointerController();
74
75public:
Jeff Brown2352b972011-04-12 22:39:53 -070076 enum InactivityTimeout {
77 INACTIVITY_TIMEOUT_NORMAL = 0,
78 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080079 };
80
Jeff Brown2352b972011-04-12 22:39:53 -070081 PointerController(const sp<PointerControllerPolicyInterface>& policy,
82 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080083
84 virtual bool getBounds(float* outMinX, float* outMinY,
85 float* outMaxX, float* outMaxY) const;
86 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070087 virtual void setButtonState(int32_t buttonState);
88 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080089 virtual void setPosition(float x, float y);
90 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -070091 virtual void fade(Transition transition);
92 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080093
Jeff Brown2352b972011-04-12 22:39:53 -070094 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -070095 virtual void setSpots(const PointerCoords* spotCoords,
96 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -070097 virtual void clearSpots();
98
Jun Mukai1db53972015-09-11 18:08:31 -070099 void updatePointerShape(int iconId);
Jeff Brownd728bf52012-09-08 18:05:28 -0700100 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700101 void setPointerIcon(const SpriteIcon& icon);
102 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800103
104private:
Jeff Brown2352b972011-04-12 22:39:53 -0700105 static const size_t MAX_RECYCLED_SPRITES = 12;
106 static const size_t MAX_SPOTS = 12;
107
Jeff Brown05dc66a2011-03-02 14:41:58 -0800108 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700109 MSG_ANIMATE,
110 MSG_INACTIVITY_TIMEOUT,
111 };
112
113 struct Spot {
114 static const uint32_t INVALID_ID = 0xffffffff;
115
116 uint32_t id;
117 sp<Sprite> sprite;
118 float alpha;
119 float scale;
120 float x, y;
121
122 inline Spot(uint32_t id, const sp<Sprite>& sprite)
123 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
124 x(0.0f), y(0.0f), lastIcon(NULL) { }
125
126 void updateSprite(const SpriteIcon* icon, float x, float y);
127
128 private:
129 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800130 };
131
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800132 mutable Mutex mLock;
133
Jeff Brown2352b972011-04-12 22:39:53 -0700134 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800135 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700136 sp<SpriteController> mSpriteController;
137 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800138
Jeff Brown2352b972011-04-12 22:39:53 -0700139 PointerResources mResources;
140
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800141 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700142 bool animationPending;
143 nsecs_t animationTime;
144
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800145 int32_t displayWidth;
146 int32_t displayHeight;
147 int32_t displayOrientation;
148
Jeff Brown2352b972011-04-12 22:39:53 -0700149 InactivityTimeout inactivityTimeout;
150
151 Presentation presentation;
152 bool presentationChanged;
153
Jeff Brown538881e2011-05-25 18:23:38 -0700154 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800155 float pointerX;
156 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700157 float pointerAlpha;
158 sp<Sprite> pointerSprite;
159 SpriteIcon pointerIcon;
160 bool pointerIconChanged;
161
Jun Mukai1db53972015-09-11 18:08:31 -0700162 std::map<int, SpriteIcon> additionalMouseResources;
163
164 int32_t requestedPointerShape;
165
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700166 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800167
Jeff Brown2352b972011-04-12 22:39:53 -0700168 Vector<Spot*> spots;
169 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700170 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800171
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800172 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
173 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800174
175 void handleMessage(const Message& message);
Jeff Brown2352b972011-04-12 22:39:53 -0700176 void doAnimate();
177 void doInactivityTimeout();
178
179 void startAnimationLocked();
180
181 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700182 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700183 void updatePointerLocked();
184
185 Spot* getSpotLocked(uint32_t id);
186 Spot* createAndAddSpotLocked(uint32_t id);
187 Spot* removeFirstFadingSpotLocked();
188 void releaseSpotLocked(Spot* spot);
189 void fadeOutAndReleaseSpotLocked(Spot* spot);
190 void fadeOutAndReleaseAllSpotsLocked();
191
192 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800193};
194
195} // namespace android
196
197#endif // _UI_POINTER_CONTROLLER_H