1 /* 2 * Copyright (C) 2020 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_CONTEXT_H 18 #define _UI_POINTER_CONTROLLER_CONTEXT_H 19 20 #include <PointerControllerInterface.h> 21 #include <gui/DisplayEventReceiver.h> 22 #include <input/DisplayViewport.h> 23 #include <input/Input.h> 24 #include <utils/BitSet.h> 25 #include <utils/Looper.h> 26 #include <utils/RefBase.h> 27 28 #include <functional> 29 #include <map> 30 #include <memory> 31 #include <vector> 32 33 #include "SpriteController.h" 34 35 namespace android { 36 37 class PointerController; 38 class MouseCursorController; 39 class TouchSpotController; 40 41 /* 42 * Pointer resources. 43 */ 44 struct PointerResources { 45 SpriteIcon spotHover; 46 SpriteIcon spotTouch; 47 SpriteIcon spotAnchor; 48 }; 49 50 struct PointerAnimation { 51 std::vector<SpriteIcon> animationFrames; 52 nsecs_t durationPerFrame; 53 }; 54 55 enum class InactivityTimeout { 56 NORMAL = 0, 57 SHORT = 1, 58 }; 59 60 /* 61 * Pointer controller policy interface. 62 * 63 * The pointer controller policy is used by the pointer controller to interact with 64 * the Window Manager and other system components. 65 * 66 * The actual implementation is partially supported by callbacks into the DVM 67 * via JNI. This interface is also mocked in the unit tests. 68 */ 69 class PointerControllerPolicyInterface : public virtual RefBase { 70 protected: PointerControllerPolicyInterface()71 PointerControllerPolicyInterface() {} ~PointerControllerPolicyInterface()72 virtual ~PointerControllerPolicyInterface() {} 73 74 public: 75 virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0; 76 virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0; 77 virtual void loadAdditionalMouseResources( 78 std::map<int32_t, SpriteIcon>* outResources, 79 std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) = 0; 80 virtual int32_t getDefaultPointerIconId() = 0; 81 virtual int32_t getCustomPointerIconId() = 0; 82 virtual void onPointerDisplayIdChanged(int32_t displayId, float xPos, float yPos) = 0; 83 }; 84 85 /* 86 * Contains logic and resources shared among PointerController, 87 * MouseCursorController, and TouchSpotController. 88 */ 89 90 class PointerControllerContext { 91 public: 92 PointerControllerContext(const sp<PointerControllerPolicyInterface>& policy, 93 const sp<Looper>& looper, const sp<SpriteController>& spriteController, 94 PointerController& controller); 95 ~PointerControllerContext(); 96 97 void removeInactivityTimeout(); 98 void resetInactivityTimeout(); 99 void startAnimation(); 100 void setInactivityTimeout(InactivityTimeout inactivityTimeout); 101 102 nsecs_t getAnimationTime(); 103 104 void clearSpotsByDisplay(int32_t displayId); 105 106 void setHandlerController(std::shared_ptr<PointerController> controller); 107 void setCallbackController(std::shared_ptr<PointerController> controller); 108 109 sp<PointerControllerPolicyInterface> getPolicy(); 110 sp<SpriteController> getSpriteController(); 111 112 void handleDisplayEvents(); 113 114 void addAnimationCallback(int32_t displayId, std::function<bool(nsecs_t)> callback); 115 void removeAnimationCallback(int32_t displayId); 116 117 class MessageHandler : public virtual android::MessageHandler { 118 public: 119 enum { 120 MSG_INACTIVITY_TIMEOUT, 121 }; 122 123 void handleMessage(const Message& message) override; 124 std::weak_ptr<PointerController> pointerController; 125 }; 126 127 class LooperCallback : public virtual android::LooperCallback { 128 public: 129 int handleEvent(int fd, int events, void* data) override; 130 std::weak_ptr<PointerController> pointerController; 131 }; 132 133 private: 134 class PointerAnimator { 135 public: 136 PointerAnimator(PointerControllerContext& context); 137 138 void addCallback(int32_t displayId, std::function<bool(nsecs_t)> callback); 139 void removeCallback(int32_t displayId); 140 void handleVsyncEvents(); 141 nsecs_t getAnimationTimeLocked(); 142 143 mutable std::mutex mLock; 144 145 private: 146 struct Locked { 147 bool animationPending{false}; 148 nsecs_t animationTime{systemTime(SYSTEM_TIME_MONOTONIC)}; 149 150 std::unordered_map<int32_t, std::function<bool(nsecs_t)>> callbacks; 151 } mLocked GUARDED_BY(mLock); 152 153 DisplayEventReceiver mDisplayEventReceiver; 154 155 PointerControllerContext& mContext; 156 157 void initializeDisplayEventReceiver(); 158 void startAnimationLocked(); 159 void handleCallbacksLocked(nsecs_t timestamp); 160 }; 161 162 sp<PointerControllerPolicyInterface> mPolicy; 163 sp<Looper> mLooper; 164 sp<SpriteController> mSpriteController; 165 sp<MessageHandler> mHandler; 166 sp<LooperCallback> mCallback; 167 168 PointerController& mController; 169 170 PointerAnimator mAnimator; 171 172 mutable std::mutex mLock; 173 174 struct Locked { 175 InactivityTimeout inactivityTimeout; 176 } mLocked GUARDED_BY(mLock); 177 178 void resetInactivityTimeoutLocked(); 179 }; 180 181 } // namespace android 182 183 #endif // _UI_POINTER_CONTROLLER_CONTEXT_H 184