Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 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 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 20 | #include "SpriteController.h" |
| 21 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 22 | #include <ui/DisplayInfo.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 23 | #include <input/Input.h> |
Michael Wright | d6b47371 | 2014-02-10 15:56:36 -0800 | [diff] [blame] | 24 | #include <inputflinger/PointerControllerInterface.h> |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 25 | #include <utils/BitSet.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 26 | #include <utils/RefBase.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 27 | #include <utils/Looper.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 28 | #include <utils/String8.h> |
| 29 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 30 | #include <SkBitmap.h> |
| 31 | |
| 32 | namespace android { |
| 33 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 34 | /* |
| 35 | * Pointer resources. |
| 36 | */ |
| 37 | struct PointerResources { |
| 38 | SpriteIcon spotHover; |
| 39 | SpriteIcon spotTouch; |
| 40 | SpriteIcon spotAnchor; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * Pointer controller policy interface. |
| 46 | * |
| 47 | * The pointer controller policy is used by the pointer controller to interact with |
| 48 | * the Window Manager and other system components. |
| 49 | * |
| 50 | * The actual implementation is partially supported by callbacks into the DVM |
| 51 | * via JNI. This interface is also mocked in the unit tests. |
| 52 | */ |
| 53 | class PointerControllerPolicyInterface : public virtual RefBase { |
| 54 | protected: |
| 55 | PointerControllerPolicyInterface() { } |
| 56 | virtual ~PointerControllerPolicyInterface() { } |
| 57 | |
| 58 | public: |
| 59 | virtual void loadPointerResources(PointerResources* outResources) = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | |
| 63 | /* |
| 64 | * Tracks pointer movements and draws the pointer sprite to a surface. |
| 65 | * |
| 66 | * Handles pointer acceleration and animation. |
| 67 | */ |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 68 | class PointerController : public PointerControllerInterface, public MessageHandler { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 69 | protected: |
| 70 | virtual ~PointerController(); |
| 71 | |
| 72 | public: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 73 | enum InactivityTimeout { |
| 74 | INACTIVITY_TIMEOUT_NORMAL = 0, |
| 75 | INACTIVITY_TIMEOUT_SHORT = 1, |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 78 | PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 79 | const sp<Looper>& looper, const sp<SpriteController>& spriteController); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 80 | |
| 81 | virtual bool getBounds(float* outMinX, float* outMinY, |
| 82 | float* outMaxX, float* outMaxY) const; |
| 83 | virtual void move(float deltaX, float deltaY); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 84 | virtual void setButtonState(int32_t buttonState); |
| 85 | virtual int32_t getButtonState() const; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 86 | virtual void setPosition(float x, float y); |
| 87 | virtual void getPosition(float* outX, float* outY) const; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 88 | virtual void fade(Transition transition); |
| 89 | virtual void unfade(Transition transition); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 90 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 91 | virtual void setPresentation(Presentation presentation); |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 92 | virtual void setSpots(const PointerCoords* spotCoords, |
| 93 | const uint32_t* spotIdToIndex, BitSet32 spotIdBits); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 94 | virtual void clearSpots(); |
| 95 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 96 | void setDisplayViewport(int32_t width, int32_t height, int32_t orientation); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 97 | void setPointerIcon(const SpriteIcon& icon); |
| 98 | void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 99 | |
| 100 | private: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 101 | static const size_t MAX_RECYCLED_SPRITES = 12; |
| 102 | static const size_t MAX_SPOTS = 12; |
| 103 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 104 | enum { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 105 | MSG_ANIMATE, |
| 106 | MSG_INACTIVITY_TIMEOUT, |
| 107 | }; |
| 108 | |
| 109 | struct Spot { |
| 110 | static const uint32_t INVALID_ID = 0xffffffff; |
| 111 | |
| 112 | uint32_t id; |
| 113 | sp<Sprite> sprite; |
| 114 | float alpha; |
| 115 | float scale; |
| 116 | float x, y; |
| 117 | |
| 118 | inline Spot(uint32_t id, const sp<Sprite>& sprite) |
| 119 | : id(id), sprite(sprite), alpha(1.0f), scale(1.0f), |
| 120 | x(0.0f), y(0.0f), lastIcon(NULL) { } |
| 121 | |
| 122 | void updateSprite(const SpriteIcon* icon, float x, float y); |
| 123 | |
| 124 | private: |
| 125 | const SpriteIcon* lastIcon; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 128 | mutable Mutex mLock; |
| 129 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 130 | sp<PointerControllerPolicyInterface> mPolicy; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 131 | sp<Looper> mLooper; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 132 | sp<SpriteController> mSpriteController; |
| 133 | sp<WeakMessageHandler> mHandler; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 134 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 135 | PointerResources mResources; |
| 136 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 137 | struct Locked { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 138 | bool animationPending; |
| 139 | nsecs_t animationTime; |
| 140 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 141 | int32_t displayWidth; |
| 142 | int32_t displayHeight; |
| 143 | int32_t displayOrientation; |
| 144 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 145 | InactivityTimeout inactivityTimeout; |
| 146 | |
| 147 | Presentation presentation; |
| 148 | bool presentationChanged; |
| 149 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 150 | int32_t pointerFadeDirection; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 151 | float pointerX; |
| 152 | float pointerY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 153 | float pointerAlpha; |
| 154 | sp<Sprite> pointerSprite; |
| 155 | SpriteIcon pointerIcon; |
| 156 | bool pointerIconChanged; |
| 157 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 158 | int32_t buttonState; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 159 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 160 | Vector<Spot*> spots; |
| 161 | Vector<sp<Sprite> > recycledSprites; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 162 | } mLocked; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 163 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 164 | bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; |
| 165 | void setPositionLocked(float x, float y); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 166 | |
| 167 | void handleMessage(const Message& message); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 168 | void doAnimate(); |
| 169 | void doInactivityTimeout(); |
| 170 | |
| 171 | void startAnimationLocked(); |
| 172 | |
| 173 | void resetInactivityTimeoutLocked(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 174 | void removeInactivityTimeoutLocked(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 175 | void updatePointerLocked(); |
| 176 | |
| 177 | Spot* getSpotLocked(uint32_t id); |
| 178 | Spot* createAndAddSpotLocked(uint32_t id); |
| 179 | Spot* removeFirstFadingSpotLocked(); |
| 180 | void releaseSpotLocked(Spot* spot); |
| 181 | void fadeOutAndReleaseSpotLocked(Spot* spot); |
| 182 | void fadeOutAndReleaseAllSpotsLocked(); |
| 183 | |
| 184 | void loadResources(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | } // namespace android |
| 188 | |
| 189 | #endif // _UI_POINTER_CONTROLLER_H |