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> |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 24 | #include <utils/BitSet.h> |
| 25 | #include <utils/Looper.h> |
| 26 | #include <utils/RefBase.h> |
| 27 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 28 | #include <functional> |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <memory> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "SpriteController.h" |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | class PointerController; |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 38 | class MouseCursorController; |
| 39 | class TouchSpotController; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 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: |
| 71 | PointerControllerPolicyInterface() {} |
| 72 | virtual ~PointerControllerPolicyInterface() {} |
| 73 | |
| 74 | public: |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 75 | virtual void loadPointerIcon(SpriteIcon* icon, ui::LogicalDisplayId displayId) = 0; |
| 76 | virtual void loadPointerResources(PointerResources* outResources, |
| 77 | ui::LogicalDisplayId displayId) = 0; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 78 | virtual void loadAdditionalMouseResources( |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 79 | std::map<PointerIconStyle, SpriteIcon>* outResources, |
| 80 | std::map<PointerIconStyle, PointerAnimation>* outAnimationResources, |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 81 | ui::LogicalDisplayId displayId) = 0; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 82 | virtual PointerIconStyle getDefaultPointerIconId() = 0; |
Seunghwan Choi | 670b33d | 2023-01-13 21:12:59 +0900 | [diff] [blame] | 83 | virtual PointerIconStyle getDefaultStylusIconId() = 0; |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 84 | virtual PointerIconStyle getCustomPointerIconId() = 0; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * Contains logic and resources shared among PointerController, |
| 89 | * MouseCursorController, and TouchSpotController. |
| 90 | */ |
| 91 | |
| 92 | class PointerControllerContext { |
| 93 | public: |
| 94 | PointerControllerContext(const sp<PointerControllerPolicyInterface>& policy, |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 95 | const sp<Looper>& looper, SpriteController& spriteController, |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 96 | PointerController& controller); |
| 97 | ~PointerControllerContext(); |
| 98 | |
| 99 | void removeInactivityTimeout(); |
| 100 | void resetInactivityTimeout(); |
| 101 | void startAnimation(); |
| 102 | void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
| 103 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 104 | nsecs_t getAnimationTime(); |
| 105 | |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 106 | void clearSpotsByDisplay(ui::LogicalDisplayId displayId); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 107 | |
| 108 | void setHandlerController(std::shared_ptr<PointerController> controller); |
| 109 | void setCallbackController(std::shared_ptr<PointerController> controller); |
| 110 | |
| 111 | sp<PointerControllerPolicyInterface> getPolicy(); |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 112 | SpriteController& getSpriteController(); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 113 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 114 | void handleDisplayEvents(); |
| 115 | |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 116 | void addAnimationCallback(ui::LogicalDisplayId displayId, |
| 117 | std::function<bool(nsecs_t)> callback); |
| 118 | void removeAnimationCallback(ui::LogicalDisplayId displayId); |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 119 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 120 | class MessageHandler : public virtual android::MessageHandler { |
| 121 | public: |
| 122 | enum { |
| 123 | MSG_INACTIVITY_TIMEOUT, |
| 124 | }; |
| 125 | |
| 126 | void handleMessage(const Message& message) override; |
| 127 | std::weak_ptr<PointerController> pointerController; |
| 128 | }; |
| 129 | |
| 130 | class LooperCallback : public virtual android::LooperCallback { |
| 131 | public: |
| 132 | int handleEvent(int fd, int events, void* data) override; |
| 133 | std::weak_ptr<PointerController> pointerController; |
| 134 | }; |
| 135 | |
| 136 | private: |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 137 | class PointerAnimator { |
| 138 | public: |
| 139 | PointerAnimator(PointerControllerContext& context); |
| 140 | |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 141 | void addCallback(ui::LogicalDisplayId displayId, std::function<bool(nsecs_t)> callback); |
| 142 | void removeCallback(ui::LogicalDisplayId displayId); |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 143 | void handleVsyncEvents(); |
| 144 | nsecs_t getAnimationTimeLocked(); |
| 145 | |
| 146 | mutable std::mutex mLock; |
| 147 | |
| 148 | private: |
| 149 | struct Locked { |
| 150 | bool animationPending{false}; |
| 151 | nsecs_t animationTime{systemTime(SYSTEM_TIME_MONOTONIC)}; |
| 152 | |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 153 | std::unordered_map<ui::LogicalDisplayId, std::function<bool(nsecs_t)>> callbacks; |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 154 | } mLocked GUARDED_BY(mLock); |
| 155 | |
| 156 | DisplayEventReceiver mDisplayEventReceiver; |
| 157 | |
| 158 | PointerControllerContext& mContext; |
| 159 | |
| 160 | void initializeDisplayEventReceiver(); |
| 161 | void startAnimationLocked(); |
| 162 | void handleCallbacksLocked(nsecs_t timestamp); |
| 163 | }; |
| 164 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 165 | sp<PointerControllerPolicyInterface> mPolicy; |
| 166 | sp<Looper> mLooper; |
Prabir Pradhan | 27c6d99 | 2023-08-18 19:44:55 +0000 | [diff] [blame] | 167 | SpriteController& mSpriteController; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 168 | sp<MessageHandler> mHandler; |
| 169 | sp<LooperCallback> mCallback; |
| 170 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 171 | PointerController& mController; |
| 172 | |
Liam Harrington | ce63713 | 2020-08-14 04:00:11 +0000 | [diff] [blame] | 173 | PointerAnimator mAnimator; |
| 174 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 175 | mutable std::mutex mLock; |
| 176 | |
| 177 | struct Locked { |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 178 | InactivityTimeout inactivityTimeout; |
| 179 | } mLocked GUARDED_BY(mLock); |
| 180 | |
| 181 | void resetInactivityTimeoutLocked(); |
| 182 | }; |
| 183 | |
| 184 | } // namespace android |
| 185 | |
| 186 | #endif // _UI_POINTER_CONTROLLER_CONTEXT_H |