blob: 6d840db0ec5c93eb9274b8edcdcc1347f23a0ede [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>
Jun Mukaic0c0ac32015-10-27 10:09:21 -070031#include <gui/DisplayEventReceiver.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033#include <SkBitmap.h>
34
35namespace android {
36
Jeff Brown2352b972011-04-12 22:39:53 -070037/*
38 * Pointer resources.
39 */
40struct PointerResources {
41 SpriteIcon spotHover;
42 SpriteIcon spotTouch;
43 SpriteIcon spotAnchor;
44};
45
Jeff Brown2352b972011-04-12 22:39:53 -070046/*
47 * Pointer controller policy interface.
48 *
49 * The pointer controller policy is used by the pointer controller to interact with
50 * the Window Manager and other system components.
51 *
52 * The actual implementation is partially supported by callbacks into the DVM
53 * via JNI. This interface is also mocked in the unit tests.
54 */
55class PointerControllerPolicyInterface : public virtual RefBase {
56protected:
57 PointerControllerPolicyInterface() { }
58 virtual ~PointerControllerPolicyInterface() { }
59
60public:
61 virtual void loadPointerResources(PointerResources* outResources) = 0;
Jun Mukai5ec74202015-10-07 16:58:09 +090062 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources) = 0;
63 virtual int32_t getDefaultPointerIconId() = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080064};
65
66
67/*
68 * Tracks pointer movements and draws the pointer sprite to a surface.
69 *
70 * Handles pointer acceleration and animation.
71 */
Jun Mukaic0c0ac32015-10-27 10:09:21 -070072class PointerController : public PointerControllerInterface, public MessageHandler,
73 public LooperCallback {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080074protected:
75 virtual ~PointerController();
76
77public:
Jeff Brown2352b972011-04-12 22:39:53 -070078 enum InactivityTimeout {
79 INACTIVITY_TIMEOUT_NORMAL = 0,
80 INACTIVITY_TIMEOUT_SHORT = 1,
Jeff Brown05dc66a2011-03-02 14:41:58 -080081 };
82
Jeff Brown2352b972011-04-12 22:39:53 -070083 PointerController(const sp<PointerControllerPolicyInterface>& policy,
84 const sp<Looper>& looper, const sp<SpriteController>& spriteController);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080085
86 virtual bool getBounds(float* outMinX, float* outMinY,
87 float* outMaxX, float* outMaxY) const;
88 virtual void move(float deltaX, float deltaY);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070089 virtual void setButtonState(int32_t buttonState);
90 virtual int32_t getButtonState() const;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080091 virtual void setPosition(float x, float y);
92 virtual void getPosition(float* outX, float* outY) const;
Jeff Brown538881e2011-05-25 18:23:38 -070093 virtual void fade(Transition transition);
94 virtual void unfade(Transition transition);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080095
Jeff Brown2352b972011-04-12 22:39:53 -070096 virtual void setPresentation(Presentation presentation);
Jeff Browncb5ffcf2011-06-06 20:03:18 -070097 virtual void setSpots(const PointerCoords* spotCoords,
98 const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
Jeff Brown2352b972011-04-12 22:39:53 -070099 virtual void clearSpots();
100
Jun Mukai1db53972015-09-11 18:08:31 -0700101 void updatePointerShape(int iconId);
Jeff Brownd728bf52012-09-08 18:05:28 -0700102 void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
Jeff Brown2352b972011-04-12 22:39:53 -0700103 void setPointerIcon(const SpriteIcon& icon);
104 void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800105
106private:
Jeff Brown2352b972011-04-12 22:39:53 -0700107 static const size_t MAX_RECYCLED_SPRITES = 12;
108 static const size_t MAX_SPOTS = 12;
109
Jeff Brown05dc66a2011-03-02 14:41:58 -0800110 enum {
Jeff Brown2352b972011-04-12 22:39:53 -0700111 MSG_INACTIVITY_TIMEOUT,
112 };
113
114 struct Spot {
115 static const uint32_t INVALID_ID = 0xffffffff;
116
117 uint32_t id;
118 sp<Sprite> sprite;
119 float alpha;
120 float scale;
121 float x, y;
122
123 inline Spot(uint32_t id, const sp<Sprite>& sprite)
124 : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
125 x(0.0f), y(0.0f), lastIcon(NULL) { }
126
127 void updateSprite(const SpriteIcon* icon, float x, float y);
128
129 private:
130 const SpriteIcon* lastIcon;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800131 };
132
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800133 mutable Mutex mLock;
134
Jeff Brown2352b972011-04-12 22:39:53 -0700135 sp<PointerControllerPolicyInterface> mPolicy;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800136 sp<Looper> mLooper;
Jeff Brown5541de92011-04-11 11:54:25 -0700137 sp<SpriteController> mSpriteController;
138 sp<WeakMessageHandler> mHandler;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800139
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700140 DisplayEventReceiver mDisplayEventReceiver;
141
Jeff Brown2352b972011-04-12 22:39:53 -0700142 PointerResources mResources;
143
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800144 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700145 bool animationPending;
146 nsecs_t animationTime;
147
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800148 int32_t displayWidth;
149 int32_t displayHeight;
150 int32_t displayOrientation;
151
Jeff Brown2352b972011-04-12 22:39:53 -0700152 InactivityTimeout inactivityTimeout;
153
154 Presentation presentation;
155 bool presentationChanged;
156
Jeff Brown538881e2011-05-25 18:23:38 -0700157 int32_t pointerFadeDirection;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800158 float pointerX;
159 float pointerY;
Jeff Brown2352b972011-04-12 22:39:53 -0700160 float pointerAlpha;
161 sp<Sprite> pointerSprite;
162 SpriteIcon pointerIcon;
163 bool pointerIconChanged;
164
Jun Mukai1db53972015-09-11 18:08:31 -0700165 std::map<int, SpriteIcon> additionalMouseResources;
166
167 int32_t requestedPointerShape;
168
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700169 int32_t buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800170
Jeff Brown2352b972011-04-12 22:39:53 -0700171 Vector<Spot*> spots;
172 Vector<sp<Sprite> > recycledSprites;
Jeff Brown5541de92011-04-11 11:54:25 -0700173 } mLocked;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800174
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800175 bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
176 void setPositionLocked(float x, float y);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800177
178 void handleMessage(const Message& message);
Jun Mukaic0c0ac32015-10-27 10:09:21 -0700179 int handleEvent(int fd, int events, void* data);
180 void doAnimate(nsecs_t timestamp);
Jeff Brown2352b972011-04-12 22:39:53 -0700181 void doInactivityTimeout();
182
183 void startAnimationLocked();
184
185 void resetInactivityTimeoutLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700186 void removeInactivityTimeoutLocked();
Jeff Brown2352b972011-04-12 22:39:53 -0700187 void updatePointerLocked();
188
189 Spot* getSpotLocked(uint32_t id);
190 Spot* createAndAddSpotLocked(uint32_t id);
191 Spot* removeFirstFadingSpotLocked();
192 void releaseSpotLocked(Spot* spot);
193 void fadeOutAndReleaseSpotLocked(Spot* spot);
194 void fadeOutAndReleaseAllSpotsLocked();
195
196 void loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800197};
198
199} // namespace android
200
201#endif // _UI_POINTER_CONTROLLER_H