Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 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 <ui/DisplayInfo.h> |
| 25 | #include <utils/BitSet.h> |
| 26 | #include <utils/Looper.h> |
| 27 | #include <utils/RefBase.h> |
| 28 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame^] | 29 | #include <functional> |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 30 | #include <map> |
| 31 | #include <memory> |
| 32 | #include <vector> |
| 33 | |
| 34 | #include "SpriteController.h" |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | class PointerController; |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame^] | 39 | class MouseCursorController; |
| 40 | class TouchSpotController; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Pointer resources. |
| 44 | */ |
| 45 | struct PointerResources { |
| 46 | SpriteIcon spotHover; |
| 47 | SpriteIcon spotTouch; |
| 48 | SpriteIcon spotAnchor; |
| 49 | }; |
| 50 | |
| 51 | struct PointerAnimation { |
| 52 | std::vector<SpriteIcon> animationFrames; |
| 53 | nsecs_t durationPerFrame; |
| 54 | }; |
| 55 | |
| 56 | enum class InactivityTimeout { |
| 57 | NORMAL = 0, |
| 58 | SHORT = 1, |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * Pointer controller policy interface. |
| 63 | * |
| 64 | * The pointer controller policy is used by the pointer controller to interact with |
| 65 | * the Window Manager and other system components. |
| 66 | * |
| 67 | * The actual implementation is partially supported by callbacks into the DVM |
| 68 | * via JNI. This interface is also mocked in the unit tests. |
| 69 | */ |
| 70 | class PointerControllerPolicyInterface : public virtual RefBase { |
| 71 | protected: |
| 72 | PointerControllerPolicyInterface() {} |
| 73 | virtual ~PointerControllerPolicyInterface() {} |
| 74 | |
| 75 | public: |
| 76 | virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId) = 0; |
| 77 | virtual void loadPointerResources(PointerResources* outResources, int32_t displayId) = 0; |
| 78 | virtual void loadAdditionalMouseResources( |
| 79 | std::map<int32_t, SpriteIcon>* outResources, |
| 80 | std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) = 0; |
| 81 | virtual int32_t getDefaultPointerIconId() = 0; |
| 82 | virtual int32_t getCustomPointerIconId() = 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 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 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 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 112 | void handleDisplayEvents(); |
| 113 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame^] | 114 | void addAnimationCallback(int32_t displayId, std::function<bool(nsecs_t)> callback); |
| 115 | void removeAnimationCallback(int32_t displayId); |
| 116 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 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: |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame^] | 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 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 162 | sp<PointerControllerPolicyInterface> mPolicy; |
| 163 | sp<Looper> mLooper; |
| 164 | sp<SpriteController> mSpriteController; |
| 165 | sp<MessageHandler> mHandler; |
| 166 | sp<LooperCallback> mCallback; |
| 167 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 168 | PointerController& mController; |
| 169 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame^] | 170 | PointerAnimator mAnimator; |
| 171 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 172 | mutable std::mutex mLock; |
| 173 | |
| 174 | struct Locked { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 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 |