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