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 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 22 | #include <map> |
| 23 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 24 | #include <ui/DisplayInfo.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 25 | #include <input/Input.h> |
Michael Wright | d6b47371 | 2014-02-10 15:56:36 -0800 | [diff] [blame] | 26 | #include <inputflinger/PointerControllerInterface.h> |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 27 | #include <utils/BitSet.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 28 | #include <utils/RefBase.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 29 | #include <utils/Looper.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 30 | #include <utils/String8.h> |
| 31 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 32 | #include <SkBitmap.h> |
| 33 | |
| 34 | namespace android { |
| 35 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Pointer resources. |
| 38 | */ |
| 39 | struct PointerResources { |
| 40 | SpriteIcon spotHover; |
| 41 | SpriteIcon spotTouch; |
| 42 | SpriteIcon spotAnchor; |
| 43 | }; |
| 44 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 45 | /* |
| 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 | */ |
| 54 | class PointerControllerPolicyInterface : public virtual RefBase { |
| 55 | protected: |
| 56 | PointerControllerPolicyInterface() { } |
| 57 | virtual ~PointerControllerPolicyInterface() { } |
| 58 | |
| 59 | public: |
| 60 | virtual void loadPointerResources(PointerResources* outResources) = 0; |
Jun Mukai | 5ec7420 | 2015-10-07 16:58:09 +0900 | [diff] [blame^] | 61 | virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources) = 0; |
| 62 | virtual int32_t getDefaultPointerIconId() = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * Tracks pointer movements and draws the pointer sprite to a surface. |
| 68 | * |
| 69 | * Handles pointer acceleration and animation. |
| 70 | */ |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 71 | class PointerController : public PointerControllerInterface, public MessageHandler { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 72 | protected: |
| 73 | virtual ~PointerController(); |
| 74 | |
| 75 | public: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 76 | enum InactivityTimeout { |
| 77 | INACTIVITY_TIMEOUT_NORMAL = 0, |
| 78 | INACTIVITY_TIMEOUT_SHORT = 1, |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 81 | PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 82 | const sp<Looper>& looper, const sp<SpriteController>& spriteController); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 83 | |
| 84 | virtual bool getBounds(float* outMinX, float* outMinY, |
| 85 | float* outMaxX, float* outMaxY) const; |
| 86 | virtual void move(float deltaX, float deltaY); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 87 | virtual void setButtonState(int32_t buttonState); |
| 88 | virtual int32_t getButtonState() const; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 89 | virtual void setPosition(float x, float y); |
| 90 | virtual void getPosition(float* outX, float* outY) const; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 91 | virtual void fade(Transition transition); |
| 92 | virtual void unfade(Transition transition); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 93 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 94 | virtual void setPresentation(Presentation presentation); |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 95 | virtual void setSpots(const PointerCoords* spotCoords, |
| 96 | const uint32_t* spotIdToIndex, BitSet32 spotIdBits); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 97 | virtual void clearSpots(); |
| 98 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 99 | void updatePointerShape(int iconId); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 100 | void setDisplayViewport(int32_t width, int32_t height, int32_t orientation); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 101 | void setPointerIcon(const SpriteIcon& icon); |
| 102 | void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 103 | |
| 104 | private: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 105 | static const size_t MAX_RECYCLED_SPRITES = 12; |
| 106 | static const size_t MAX_SPOTS = 12; |
| 107 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 108 | enum { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 109 | 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 Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 132 | mutable Mutex mLock; |
| 133 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 134 | sp<PointerControllerPolicyInterface> mPolicy; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 135 | sp<Looper> mLooper; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 136 | sp<SpriteController> mSpriteController; |
| 137 | sp<WeakMessageHandler> mHandler; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 138 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 139 | PointerResources mResources; |
| 140 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 141 | struct Locked { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 142 | bool animationPending; |
| 143 | nsecs_t animationTime; |
| 144 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 145 | int32_t displayWidth; |
| 146 | int32_t displayHeight; |
| 147 | int32_t displayOrientation; |
| 148 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 149 | InactivityTimeout inactivityTimeout; |
| 150 | |
| 151 | Presentation presentation; |
| 152 | bool presentationChanged; |
| 153 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 154 | int32_t pointerFadeDirection; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 155 | float pointerX; |
| 156 | float pointerY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 157 | float pointerAlpha; |
| 158 | sp<Sprite> pointerSprite; |
| 159 | SpriteIcon pointerIcon; |
| 160 | bool pointerIconChanged; |
| 161 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 162 | std::map<int, SpriteIcon> additionalMouseResources; |
| 163 | |
| 164 | int32_t requestedPointerShape; |
| 165 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 166 | int32_t buttonState; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 167 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 168 | Vector<Spot*> spots; |
| 169 | Vector<sp<Sprite> > recycledSprites; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 170 | } mLocked; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 171 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 172 | bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; |
| 173 | void setPositionLocked(float x, float y); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 174 | |
| 175 | void handleMessage(const Message& message); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 176 | void doAnimate(); |
| 177 | void doInactivityTimeout(); |
| 178 | |
| 179 | void startAnimationLocked(); |
| 180 | |
| 181 | void resetInactivityTimeoutLocked(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 182 | void removeInactivityTimeoutLocked(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 183 | 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 Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | } // namespace android |
| 196 | |
| 197 | #endif // _UI_POINTER_CONTROLLER_H |