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> |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 23 | #include <vector> |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 24 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 25 | #include <ui/DisplayInfo.h> |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 26 | #include <input/DisplayViewport.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 27 | #include <input/Input.h> |
Prabir Pradhan | e5696a5 | 2018-11-14 19:55:21 -0800 | [diff] [blame] | 28 | #include <PointerControllerInterface.h> |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 29 | #include <utils/BitSet.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 30 | #include <utils/RefBase.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 31 | #include <utils/Looper.h> |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 32 | #include <gui/DisplayEventReceiver.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 33 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 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 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 45 | struct PointerAnimation { |
| 46 | std::vector<SpriteIcon> animationFrames; |
| 47 | nsecs_t durationPerFrame; |
| 48 | }; |
| 49 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Pointer controller policy interface. |
| 52 | * |
| 53 | * The pointer controller policy is used by the pointer controller to interact with |
| 54 | * the Window Manager and other system components. |
| 55 | * |
| 56 | * The actual implementation is partially supported by callbacks into the DVM |
| 57 | * via JNI. This interface is also mocked in the unit tests. |
| 58 | */ |
| 59 | class PointerControllerPolicyInterface : public virtual RefBase { |
| 60 | protected: |
| 61 | PointerControllerPolicyInterface() { } |
| 62 | virtual ~PointerControllerPolicyInterface() { } |
| 63 | |
| 64 | public: |
Andrii Kulian | fd8666d | 2018-10-05 16:58:39 -0700 | [diff] [blame] | 65 | virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0; |
| 66 | virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0; |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 67 | virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources, |
Andrii Kulian | fd8666d | 2018-10-05 16:58:39 -0700 | [diff] [blame] | 68 | std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) = 0; |
Jun Mukai | 5ec7420 | 2015-10-07 16:58:09 +0900 | [diff] [blame] | 69 | virtual int32_t getDefaultPointerIconId() = 0; |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 70 | virtual int32_t getCustomPointerIconId() = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | * Tracks pointer movements and draws the pointer sprite to a surface. |
| 76 | * |
| 77 | * Handles pointer acceleration and animation. |
| 78 | */ |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 79 | class PointerController : public PointerControllerInterface, public MessageHandler, |
| 80 | public LooperCallback { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 81 | protected: |
| 82 | virtual ~PointerController(); |
| 83 | |
| 84 | public: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 85 | enum InactivityTimeout { |
| 86 | INACTIVITY_TIMEOUT_NORMAL = 0, |
| 87 | INACTIVITY_TIMEOUT_SHORT = 1, |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 90 | PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 91 | const sp<Looper>& looper, const sp<SpriteController>& spriteController); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 92 | |
| 93 | virtual bool getBounds(float* outMinX, float* outMinY, |
| 94 | float* outMaxX, float* outMaxY) const; |
| 95 | virtual void move(float deltaX, float deltaY); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 96 | virtual void setButtonState(int32_t buttonState); |
| 97 | virtual int32_t getButtonState() const; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 98 | virtual void setPosition(float x, float y); |
| 99 | virtual void getPosition(float* outX, float* outY) const; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 100 | virtual int32_t getDisplayId() const; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 101 | virtual void fade(Transition transition); |
| 102 | virtual void unfade(Transition transition); |
Garfield Tan | 68ede07 | 2020-01-09 11:30:04 -0800 | [diff] [blame] | 103 | virtual void setDisplayViewport(const DisplayViewport& viewport); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 104 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 105 | virtual void setPresentation(Presentation presentation); |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 106 | virtual void setSpots(const PointerCoords* spotCoords, |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 107 | const uint32_t* spotIdToIndex, BitSet32 spotIdBits, int32_t displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 108 | virtual void clearSpots(); |
| 109 | |
Michael Wright | f9d9ce77 | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 110 | void updatePointerIcon(int32_t iconId); |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 111 | void setCustomPointerIcon(const SpriteIcon& icon); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 112 | void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 113 | void reloadPointerResources(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 114 | |
| 115 | private: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 116 | static const size_t MAX_RECYCLED_SPRITES = 12; |
| 117 | static const size_t MAX_SPOTS = 12; |
| 118 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 119 | enum { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 120 | MSG_INACTIVITY_TIMEOUT, |
| 121 | }; |
| 122 | |
| 123 | struct Spot { |
| 124 | static const uint32_t INVALID_ID = 0xffffffff; |
| 125 | |
| 126 | uint32_t id; |
| 127 | sp<Sprite> sprite; |
| 128 | float alpha; |
| 129 | float scale; |
| 130 | float x, y; |
| 131 | |
| 132 | inline Spot(uint32_t id, const sp<Sprite>& sprite) |
| 133 | : id(id), sprite(sprite), alpha(1.0f), scale(1.0f), |
| 134 | x(0.0f), y(0.0f), lastIcon(NULL) { } |
| 135 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 136 | void updateSprite(const SpriteIcon* icon, float x, float y, int32_t displayId); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | const SpriteIcon* lastIcon; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 142 | mutable Mutex mLock; |
| 143 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 144 | sp<PointerControllerPolicyInterface> mPolicy; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 145 | sp<Looper> mLooper; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 146 | sp<SpriteController> mSpriteController; |
| 147 | sp<WeakMessageHandler> mHandler; |
Vladislav Kaznacheev | 33c5903 | 2016-09-09 10:03:31 -0700 | [diff] [blame] | 148 | sp<LooperCallback> mCallback; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 149 | |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 150 | DisplayEventReceiver mDisplayEventReceiver; |
| 151 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 152 | PointerResources mResources; |
| 153 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 154 | struct Locked { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 155 | bool animationPending; |
| 156 | nsecs_t animationTime; |
| 157 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 158 | size_t animationFrameIndex; |
| 159 | nsecs_t lastFrameUpdatedTime; |
| 160 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 161 | DisplayViewport viewport; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 162 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 163 | InactivityTimeout inactivityTimeout; |
| 164 | |
| 165 | Presentation presentation; |
| 166 | bool presentationChanged; |
| 167 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 168 | int32_t pointerFadeDirection; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 169 | float pointerX; |
| 170 | float pointerY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 171 | float pointerAlpha; |
| 172 | sp<Sprite> pointerSprite; |
| 173 | SpriteIcon pointerIcon; |
| 174 | bool pointerIconChanged; |
| 175 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 176 | std::map<int32_t, SpriteIcon> additionalMouseResources; |
| 177 | std::map<int32_t, PointerAnimation> animationResources; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 178 | |
Michael Wright | f9d9ce77 | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 179 | int32_t requestedPointerType; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 180 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 181 | int32_t buttonState; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 182 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 183 | std::map<int32_t /* displayId */, std::vector<Spot*>> spotsByDisplay; |
| 184 | std::vector<sp<Sprite> > recycledSprites; |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 185 | } mLocked GUARDED_BY(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 186 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 187 | bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; |
| 188 | void setPositionLocked(float x, float y); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 189 | |
| 190 | void handleMessage(const Message& message); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 191 | int handleEvent(int fd, int events, void* data); |
| 192 | void doAnimate(nsecs_t timestamp); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 193 | bool doFadingAnimationLocked(nsecs_t timestamp); |
| 194 | bool doBitmapAnimationLocked(nsecs_t timestamp); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 195 | void doInactivityTimeout(); |
| 196 | |
| 197 | void startAnimationLocked(); |
| 198 | |
| 199 | void resetInactivityTimeoutLocked(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 200 | void removeInactivityTimeoutLocked(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 201 | void updatePointerLocked(); |
| 202 | |
Arthur Hung | d25699a | 2019-01-25 17:53:22 +0800 | [diff] [blame] | 203 | Spot* getSpot(uint32_t id, const std::vector<Spot*>& spots); |
| 204 | Spot* createAndAddSpotLocked(uint32_t id, std::vector<Spot*>& spots); |
| 205 | Spot* removeFirstFadingSpotLocked(std::vector<Spot*>& spots); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 206 | void releaseSpotLocked(Spot* spot); |
| 207 | void fadeOutAndReleaseSpotLocked(Spot* spot); |
| 208 | void fadeOutAndReleaseAllSpotsLocked(); |
| 209 | |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 210 | void loadResourcesLocked(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } // namespace android |
| 214 | |
| 215 | #endif // _UI_POINTER_CONTROLLER_H |