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