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> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 26 | #include <input/Input.h> |
Michael Wright | d6b47371 | 2014-02-10 15:56:36 -0800 | [diff] [blame] | 27 | #include <inputflinger/PointerControllerInterface.h> |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 28 | #include <utils/BitSet.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 29 | #include <utils/RefBase.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 30 | #include <utils/Looper.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 31 | #include <utils/String8.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: |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame^] | 65 | virtual void loadPointerIcon(SpriteIcon* icon) = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 66 | virtual void loadPointerResources(PointerResources* outResources) = 0; |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 67 | virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources, |
| 68 | std::map<int32_t, PointerAnimation>* outAnimationResources) = 0; |
Jun Mukai | 5ec7420 | 2015-10-07 16:58:09 +0900 | [diff] [blame] | 69 | virtual int32_t getDefaultPointerIconId() = 0; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * Tracks pointer movements and draws the pointer sprite to a surface. |
| 75 | * |
| 76 | * Handles pointer acceleration and animation. |
| 77 | */ |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 78 | class PointerController : public PointerControllerInterface, public MessageHandler, |
| 79 | public LooperCallback { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 80 | protected: |
| 81 | virtual ~PointerController(); |
| 82 | |
| 83 | public: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 84 | enum InactivityTimeout { |
| 85 | INACTIVITY_TIMEOUT_NORMAL = 0, |
| 86 | INACTIVITY_TIMEOUT_SHORT = 1, |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 89 | PointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 90 | const sp<Looper>& looper, const sp<SpriteController>& spriteController); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 91 | |
| 92 | virtual bool getBounds(float* outMinX, float* outMinY, |
| 93 | float* outMaxX, float* outMaxY) const; |
| 94 | virtual void move(float deltaX, float deltaY); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 95 | virtual void setButtonState(int32_t buttonState); |
| 96 | virtual int32_t getButtonState() const; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 97 | virtual void setPosition(float x, float y); |
| 98 | virtual void getPosition(float* outX, float* outY) 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); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 101 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 102 | virtual void setPresentation(Presentation presentation); |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 103 | virtual void setSpots(const PointerCoords* spotCoords, |
| 104 | const uint32_t* spotIdToIndex, BitSet32 spotIdBits); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 105 | virtual void clearSpots(); |
| 106 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 107 | void updatePointerShape(int32_t iconId); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 108 | void setDisplayViewport(int32_t width, int32_t height, int32_t orientation); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 109 | void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame^] | 110 | void reloadPointerResources(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 113 | static const size_t MAX_RECYCLED_SPRITES = 12; |
| 114 | static const size_t MAX_SPOTS = 12; |
| 115 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 116 | enum { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 117 | MSG_INACTIVITY_TIMEOUT, |
| 118 | }; |
| 119 | |
| 120 | struct Spot { |
| 121 | static const uint32_t INVALID_ID = 0xffffffff; |
| 122 | |
| 123 | uint32_t id; |
| 124 | sp<Sprite> sprite; |
| 125 | float alpha; |
| 126 | float scale; |
| 127 | float x, y; |
| 128 | |
| 129 | inline Spot(uint32_t id, const sp<Sprite>& sprite) |
| 130 | : id(id), sprite(sprite), alpha(1.0f), scale(1.0f), |
| 131 | x(0.0f), y(0.0f), lastIcon(NULL) { } |
| 132 | |
| 133 | void updateSprite(const SpriteIcon* icon, float x, float y); |
| 134 | |
| 135 | private: |
| 136 | const SpriteIcon* lastIcon; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 139 | mutable Mutex mLock; |
| 140 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 141 | sp<PointerControllerPolicyInterface> mPolicy; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 142 | sp<Looper> mLooper; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 143 | sp<SpriteController> mSpriteController; |
| 144 | sp<WeakMessageHandler> mHandler; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 145 | |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 146 | DisplayEventReceiver mDisplayEventReceiver; |
| 147 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 148 | PointerResources mResources; |
| 149 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 150 | struct Locked { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 151 | bool animationPending; |
| 152 | nsecs_t animationTime; |
| 153 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 154 | size_t animationFrameIndex; |
| 155 | nsecs_t lastFrameUpdatedTime; |
| 156 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 157 | int32_t displayWidth; |
| 158 | int32_t displayHeight; |
| 159 | int32_t displayOrientation; |
| 160 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 161 | InactivityTimeout inactivityTimeout; |
| 162 | |
| 163 | Presentation presentation; |
| 164 | bool presentationChanged; |
| 165 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 166 | int32_t pointerFadeDirection; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 167 | float pointerX; |
| 168 | float pointerY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 169 | float pointerAlpha; |
| 170 | sp<Sprite> pointerSprite; |
| 171 | SpriteIcon pointerIcon; |
| 172 | bool pointerIconChanged; |
| 173 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 174 | std::map<int32_t, SpriteIcon> additionalMouseResources; |
| 175 | std::map<int32_t, PointerAnimation> animationResources; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 176 | |
| 177 | int32_t requestedPointerShape; |
| 178 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 179 | int32_t buttonState; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 180 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 181 | Vector<Spot*> spots; |
| 182 | Vector<sp<Sprite> > recycledSprites; |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 183 | } mLocked; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 184 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 185 | bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const; |
| 186 | void setPositionLocked(float x, float y); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 187 | |
| 188 | void handleMessage(const Message& message); |
Jun Mukai | c0c0ac3 | 2015-10-27 10:09:21 -0700 | [diff] [blame] | 189 | int handleEvent(int fd, int events, void* data); |
| 190 | void doAnimate(nsecs_t timestamp); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 191 | bool doFadingAnimationLocked(nsecs_t timestamp); |
| 192 | bool doBitmapAnimationLocked(nsecs_t timestamp); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 193 | void doInactivityTimeout(); |
| 194 | |
| 195 | void startAnimationLocked(); |
| 196 | |
| 197 | void resetInactivityTimeoutLocked(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 198 | void removeInactivityTimeoutLocked(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 199 | void updatePointerLocked(); |
| 200 | |
| 201 | Spot* getSpotLocked(uint32_t id); |
| 202 | Spot* createAndAddSpotLocked(uint32_t id); |
| 203 | Spot* removeFirstFadingSpotLocked(); |
| 204 | void releaseSpotLocked(Spot* spot); |
| 205 | void fadeOutAndReleaseSpotLocked(Spot* spot); |
| 206 | void fadeOutAndReleaseAllSpotsLocked(); |
| 207 | |
| 208 | void loadResources(); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | } // namespace android |
| 212 | |
| 213 | #endif // _UI_POINTER_CONTROLLER_H |