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> |
Patrick Williams | 8e47a67 | 2023-05-01 11:30:37 -0500 | [diff] [blame] | 22 | #include <gui/WindowInfosUpdate.h> |
Arthur Hung | b9b3200 | 2018-12-18 17:39:43 +0800 | [diff] [blame] | 23 | #include <input/DisplayViewport.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 24 | #include <input/Input.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> |
Michael Wright | 72a8913 | 2022-10-22 03:16:31 +0100 | [diff] [blame] | 31 | #include <string> |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 34 | #include "MouseCursorController.h" |
| 35 | #include "PointerControllerContext.h" |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 36 | #include "SpriteController.h" |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 37 | #include "TouchSpotController.h" |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 38 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 41 | /* |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 42 | * Tracks pointer movements and draws the pointer sprite to a surface. |
| 43 | * |
| 44 | * Handles pointer acceleration and animation. |
| 45 | */ |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 46 | class PointerController : public PointerControllerInterface { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 47 | public: |
Michael Wright | a0bc6b1 | 2020-06-26 20:25:34 +0100 | [diff] [blame] | 48 | static std::shared_ptr<PointerController> create( |
| 49 | const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 50 | SpriteController& spriteController, bool enabled, |
| 51 | ControllerType type = ControllerType::LEGACY); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 52 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 53 | ~PointerController() override; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 54 | |
Siarhei Vishniakou | a21a549 | 2023-03-15 09:29:53 -0700 | [diff] [blame] | 55 | std::optional<FloatRect> getBounds() const override; |
| 56 | void move(float deltaX, float deltaY) override; |
| 57 | void setPosition(float x, float y) override; |
| 58 | FloatPoint getPosition() const override; |
| 59 | int32_t getDisplayId() const override; |
| 60 | void fade(Transition transition) override; |
| 61 | void unfade(Transition transition) override; |
| 62 | void setDisplayViewport(const DisplayViewport& viewport) override; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 63 | |
Siarhei Vishniakou | a21a549 | 2023-03-15 09:29:53 -0700 | [diff] [blame] | 64 | void setPresentation(Presentation presentation) override; |
| 65 | void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
| 66 | BitSet32 spotIdBits, int32_t displayId) override; |
| 67 | void clearSpots() override; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 68 | |
Brandon Pollack | 015f5d9 | 2022-06-02 06:59:33 +0000 | [diff] [blame] | 69 | void updatePointerIcon(PointerIconStyle iconId); |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 70 | void setCustomPointerIcon(const SpriteIcon& icon); |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 71 | virtual void setInactivityTimeout(InactivityTimeout inactivityTimeout); |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 72 | void doInactivityTimeout(); |
Jun Mukai | 19a5601 | 2015-11-24 11:25:52 -0800 | [diff] [blame] | 73 | void reloadPointerResources(); |
Prabir Pradhan | c220018 | 2023-06-09 23:39:15 +0000 | [diff] [blame] | 74 | void onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 75 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 76 | void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos) |
| 77 | REQUIRES(getLock()); |
| 78 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 79 | std::string dump() override; |
Michael Wright | 72a8913 | 2022-10-22 03:16:31 +0100 | [diff] [blame] | 80 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 81 | protected: |
| 82 | using WindowListenerConsumer = |
| 83 | std::function<void(const sp<android::gui::WindowInfosListener>&)>; |
| 84 | |
| 85 | // Constructor used to test WindowInfosListener registration. |
| 86 | PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
Prabir Pradhan | 3401d23 | 2023-06-15 23:15:32 +0000 | [diff] [blame] | 87 | SpriteController& spriteController, bool enabled, |
| 88 | WindowListenerConsumer registerListener, |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 89 | WindowListenerConsumer unregisterListener); |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 90 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 91 | PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
Prabir Pradhan | 3401d23 | 2023-06-15 23:15:32 +0000 | [diff] [blame] | 92 | SpriteController& spriteController, bool enabled); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 93 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 94 | private: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 95 | friend PointerControllerContext::LooperCallback; |
| 96 | friend PointerControllerContext::MessageHandler; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 97 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 98 | // PointerController's DisplayInfoListener can outlive the PointerController because when the |
| 99 | // listener is registered, a strong pointer to the listener (which can extend its lifecycle) |
| 100 | // is given away. To avoid the small overhead of using two separate locks in these two objects, |
| 101 | // we use the DisplayInfoListener's lock in PointerController. |
| 102 | std::mutex& getLock() const; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 103 | |
Prabir Pradhan | 3401d23 | 2023-06-15 23:15:32 +0000 | [diff] [blame] | 104 | const bool mEnabled; |
| 105 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 106 | PointerControllerContext mContext; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 107 | |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 108 | MouseCursorController mCursorController; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 109 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 110 | struct Locked { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 111 | Presentation presentation; |
Prabir Pradhan | 0e3d665 | 2022-03-10 14:39:46 +0000 | [diff] [blame] | 112 | int32_t pointerDisplayId = ADISPLAY_ID_NONE; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 113 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 114 | std::vector<gui::DisplayInfo> mDisplayInfos; |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 115 | std::unordered_map<int32_t /* displayId */, TouchSpotController> spotControllers; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 116 | } mLocked GUARDED_BY(getLock()); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 117 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 118 | class DisplayInfoListener : public gui::WindowInfosListener { |
| 119 | public: |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 120 | explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){}; |
Patrick Williams | 8e47a67 | 2023-05-01 11:30:37 -0500 | [diff] [blame] | 121 | void onWindowInfosChanged(const gui::WindowInfosUpdate&) override; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 122 | void onPointerControllerDestroyed(); |
| 123 | |
| 124 | // This lock is also used by PointerController. See PointerController::getLock(). |
| 125 | std::mutex mLock; |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 126 | |
| 127 | private: |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 128 | PointerController* mPointerController GUARDED_BY(mLock); |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 129 | }; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 130 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 131 | sp<DisplayInfoListener> mDisplayInfoListener; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 132 | const WindowListenerConsumer mUnregisterWindowInfosListener; |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 133 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 134 | const ui::Transform& getTransformForDisplayLocked(int displayId) const REQUIRES(getLock()); |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 135 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 136 | void clearSpotsLocked() REQUIRES(getLock()); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 139 | class MousePointerController : public PointerController { |
| 140 | public: |
| 141 | /** A version of PointerController that controls one mouse pointer. */ |
| 142 | MousePointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 143 | const sp<Looper>& looper, SpriteController& spriteController, |
| 144 | bool enabled); |
| 145 | |
Prabir Pradhan | b0e2807 | 2023-11-08 21:09:34 +0000 | [diff] [blame^] | 146 | ~MousePointerController() override; |
| 147 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 148 | void setPresentation(Presentation) override { |
| 149 | LOG_ALWAYS_FATAL("Should not be called"); |
| 150 | } |
| 151 | void setSpots(const PointerCoords*, const uint32_t*, BitSet32, int32_t) override { |
| 152 | LOG_ALWAYS_FATAL("Should not be called"); |
| 153 | } |
| 154 | void clearSpots() override { |
| 155 | LOG_ALWAYS_FATAL("Should not be called"); |
| 156 | } |
| 157 | }; |
| 158 | |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 159 | class TouchPointerController : public PointerController { |
| 160 | public: |
| 161 | /** A version of PointerController that controls touch spots. */ |
| 162 | TouchPointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 163 | const sp<Looper>& looper, SpriteController& spriteController, |
| 164 | bool enabled); |
| 165 | |
Prabir Pradhan | b0e2807 | 2023-11-08 21:09:34 +0000 | [diff] [blame^] | 166 | ~TouchPointerController() override; |
| 167 | |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 168 | std::optional<FloatRect> getBounds() const override { |
| 169 | LOG_ALWAYS_FATAL("Should not be called"); |
| 170 | } |
| 171 | void move(float, float) override { |
| 172 | LOG_ALWAYS_FATAL("Should not be called"); |
| 173 | } |
| 174 | void setPosition(float, float) override { |
| 175 | LOG_ALWAYS_FATAL("Should not be called"); |
| 176 | } |
| 177 | FloatPoint getPosition() const override { |
| 178 | LOG_ALWAYS_FATAL("Should not be called"); |
| 179 | } |
| 180 | int32_t getDisplayId() const override { |
| 181 | LOG_ALWAYS_FATAL("Should not be called"); |
| 182 | } |
| 183 | void fade(Transition) override { |
| 184 | LOG_ALWAYS_FATAL("Should not be called"); |
| 185 | } |
| 186 | void unfade(Transition) override { |
| 187 | LOG_ALWAYS_FATAL("Should not be called"); |
| 188 | } |
| 189 | void setDisplayViewport(const DisplayViewport&) override { |
| 190 | LOG_ALWAYS_FATAL("Should not be called"); |
| 191 | } |
| 192 | void setPresentation(Presentation) override { |
| 193 | LOG_ALWAYS_FATAL("Should not be called"); |
| 194 | } |
| 195 | void updatePointerIcon(PointerIconStyle) { |
| 196 | LOG_ALWAYS_FATAL("Should not be called"); |
| 197 | } |
| 198 | void setCustomPointerIcon(const SpriteIcon&) { |
| 199 | LOG_ALWAYS_FATAL("Should not be called"); |
| 200 | } |
| 201 | // fade() should not be called by inactivity timeout. Do nothing. |
| 202 | void setInactivityTimeout(InactivityTimeout) override {} |
| 203 | }; |
| 204 | |
Byoungho Jung | 08daa25 | 2023-10-27 20:55:18 +0900 | [diff] [blame] | 205 | class StylusPointerController : public PointerController { |
| 206 | public: |
| 207 | /** A version of PointerController that controls one stylus pointer. */ |
| 208 | StylusPointerController(const sp<PointerControllerPolicyInterface>& policy, |
| 209 | const sp<Looper>& looper, SpriteController& spriteController, |
| 210 | bool enabled); |
| 211 | |
Prabir Pradhan | b0e2807 | 2023-11-08 21:09:34 +0000 | [diff] [blame^] | 212 | ~StylusPointerController() override; |
| 213 | |
Byoungho Jung | 08daa25 | 2023-10-27 20:55:18 +0900 | [diff] [blame] | 214 | void setPresentation(Presentation) override { |
| 215 | LOG_ALWAYS_FATAL("Should not be called"); |
| 216 | } |
| 217 | void setSpots(const PointerCoords*, const uint32_t*, BitSet32, int32_t) override { |
| 218 | LOG_ALWAYS_FATAL("Should not be called"); |
| 219 | } |
| 220 | void clearSpots() override { |
| 221 | LOG_ALWAYS_FATAL("Should not be called"); |
| 222 | } |
| 223 | }; |
| 224 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 225 | } // namespace android |
| 226 | |
| 227 | #endif // _UI_POINTER_CONTROLLER_H |