blob: 308ff124206405f545fcc035b787751a273ec301 [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 Mukai1db53972015-09-11 18:08:31 -070061 virtual void loadAdditionalMouseResources(std::map<int, SpriteIcon>* outResources) = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080062};
63
64
65/*
66 * Tracks pointer movements and draws the pointer sprite to a surface.
67 *
68 * Handles pointer acceleration and animation.
69 */
Jeff Brown05dc66a2011-03-02 14:41:58 -080070class PointerController : public PointerControllerInterface, public MessageHandler {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080071protected:
72 virtual ~PointerController();
73
74public:
Jeff Brown2352b972011-04-12 22:39:53 -070075 enum InactivityTimeout {
76 INACTIVITY_TIMEOUT_NORMAL = 0,
77 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080078 };
79
Jeff Brown2352b972011-04-12 22:39:53 -070080 PointerController(const sp<PointerControllerPolicyInterface>& policy,
81 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080082
83 virtual bool getBounds(float* outMinX, float* outMinY,
84 float* outMaxX, float* outMaxY) const;
85 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070086 virtual void setButtonState(int32_t buttonState);
87 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080088 virtual void setPosition(float x, float y);
89 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -070090 virtual void fade(Transition transition);
91 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080092
Jeff Brown2352b972011-04-12 22:39:53 -070093 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -070094 virtual void setSpots(const PointerCoords* spotCoords,
95 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -070096 virtual void clearSpots();
97
Jun Mukai1db53972015-09-11 18:08:31 -070098 void updatePointerShape(int iconId);
Jeff Brownd728bf52012-09-08 18:05:28 -070099 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700100 void setPointerIcon(const SpriteIcon& icon);
101 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800102
103private:
Jeff Brown2352b972011-04-12 22:39:53 -0700104 static const size_t MAX_RECYCLED_SPRITES = 12;
105 static const size_t MAX_SPOTS = 12;
106
Jeff Brown05dc66a2011-03-02 14:41:58 -0800107 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700108 MSG_ANIMATE,
109 MSG_INACTIVITY_TIMEOUT,
110 };
111
112 struct Spot {
113 static const uint32_t INVALID_ID = 0xffffffff;
114
115 uint32_t id;
116 sp<Sprite> sprite;
117 float alpha;
118 float scale;
119 float x, y;
120
121 inline Spot(uint32_t id, const sp<Sprite>& sprite)
122 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
123 x(0.0f), y(0.0f), lastIcon(NULL) { }
124
125 void updateSprite(const SpriteIcon* icon, float x, float y);
126
127 private:
128 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800129 };
130
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800131 mutable Mutex mLock;
132
Jeff Brown2352b972011-04-12 22:39:53 -0700133 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800134 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700135 sp<SpriteController> mSpriteController;
136 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800137
Jeff Brown2352b972011-04-12 22:39:53 -0700138 PointerResources mResources;
139
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800140 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700141 bool animationPending;
142 nsecs_t animationTime;
143
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800144 int32_t displayWidth;
145 int32_t displayHeight;
146 int32_t displayOrientation;
147
Jeff Brown2352b972011-04-12 22:39:53 -0700148 InactivityTimeout inactivityTimeout;
149
150 Presentation presentation;
151 bool presentationChanged;
152
Jeff Brown538881e2011-05-25 18:23:38 -0700153 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800154 float pointerX;
155 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700156 float pointerAlpha;
157 sp<Sprite> pointerSprite;
158 SpriteIcon pointerIcon;
159 bool pointerIconChanged;
160
Jun Mukai1db53972015-09-11 18:08:31 -0700161 std::map<int, SpriteIcon> additionalMouseResources;
162
163 int32_t requestedPointerShape;
164
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700165 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800166
Jeff Brown2352b972011-04-12 22:39:53 -0700167 Vector<Spot*> spots;
168 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700169 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800170
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800171 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
172 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800173
174 void handleMessage(const Message& message);
Jeff Brown2352b972011-04-12 22:39:53 -0700175 void doAnimate();
176 void doInactivityTimeout();
177
178 void startAnimationLocked();
179
180 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700181 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700182 void updatePointerLocked();
183
184 Spot* getSpotLocked(uint32_t id);
185 Spot* createAndAddSpotLocked(uint32_t id);
186 Spot* removeFirstFadingSpotLocked();
187 void releaseSpotLocked(Spot* spot);
188 void fadeOutAndReleaseSpotLocked(Spot* spot);
189 void fadeOutAndReleaseAllSpotsLocked();
190
191 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800192};
193
194} // namespace android
195
196#endif // _UI_POINTER_CONTROLLER_H