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, |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 50 | SpriteController& spriteController, ControllerType type); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 51 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 52 | ~PointerController() override; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 53 | |
Siarhei Vishniakou | a21a549 | 2023-03-15 09:29:53 -0700 | [diff] [blame] | 54 | void move(float deltaX, float deltaY) override; |
| 55 | void setPosition(float x, float y) override; |
| 56 | FloatPoint getPosition() const override; |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 57 | ui::LogicalDisplayId getDisplayId() const override; |
Siarhei Vishniakou | a21a549 | 2023-03-15 09:29:53 -0700 | [diff] [blame] | 58 | void fade(Transition transition) override; |
| 59 | void unfade(Transition transition) override; |
| 60 | void setDisplayViewport(const DisplayViewport& viewport) override; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 61 | |
Siarhei Vishniakou | a21a549 | 2023-03-15 09:29:53 -0700 | [diff] [blame] | 62 | void setPresentation(Presentation presentation) override; |
| 63 | void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 64 | BitSet32 spotIdBits, ui::LogicalDisplayId displayId) override; |
Siarhei Vishniakou | a21a549 | 2023-03-15 09:29:53 -0700 | [diff] [blame] | 65 | void clearSpots() override; |
Byoungho Jung | 1a1571d | 2023-11-03 20:12:50 +0900 | [diff] [blame] | 66 | void updatePointerIcon(PointerIconStyle iconId) override; |
| 67 | void setCustomPointerIcon(const SpriteIcon& icon) override; |
Arpit Singh | f4ae0ac | 2024-03-26 18:41:06 +0000 | [diff] [blame] | 68 | void setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) override; |
| 69 | void clearSkipScreenshotFlags() override; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 70 | |
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: |
Prabir Pradhan | bdf9369 | 2024-01-23 18:08:28 +0000 | [diff] [blame] | 82 | using WindowListenerRegisterConsumer = std::function<std::vector<gui::DisplayInfo>( |
| 83 | const sp<android::gui::WindowInfosListener>&)>; |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame] | 84 | using WindowListenerUnregisterConsumer = |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 85 | std::function<void(const sp<android::gui::WindowInfosListener>&)>; |
| 86 | |
| 87 | // Constructor used to test WindowInfosListener registration. |
| 88 | PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 89 | SpriteController& spriteController, |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame] | 90 | const WindowListenerRegisterConsumer& registerListener, |
| 91 | WindowListenerUnregisterConsumer unregisterListener); |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 92 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 93 | PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 94 | SpriteController& spriteController); |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 95 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 96 | private: |
Liam Harrington | c782be6 | 2020-07-17 19:48:24 +0000 | [diff] [blame] | 97 | friend PointerControllerContext::LooperCallback; |
| 98 | friend PointerControllerContext::MessageHandler; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 99 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 100 | // PointerController's DisplayInfoListener can outlive the PointerController because when the |
| 101 | // listener is registered, a strong pointer to the listener (which can extend its lifecycle) |
| 102 | // is given away. To avoid the small overhead of using two separate locks in these two objects, |
| 103 | // we use the DisplayInfoListener's lock in PointerController. |
| 104 | std::mutex& getLock() const; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 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; |
Siarhei Vishniakou | 445af18 | 2024-05-13 13:17:05 -0700 | [diff] [blame] | 112 | ui::LogicalDisplayId pointerDisplayId = ui::LogicalDisplayId::INVALID; |
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; |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 115 | std::unordered_map<ui::LogicalDisplayId, TouchSpotController> spotControllers; |
| 116 | std::unordered_set<ui::LogicalDisplayId> displaysToSkipScreenshot; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 117 | } mLocked GUARDED_BY(getLock()); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 118 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 119 | class DisplayInfoListener : public gui::WindowInfosListener { |
| 120 | public: |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 121 | explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){}; |
Patrick Williams | 8e47a67 | 2023-05-01 11:30:37 -0500 | [diff] [blame] | 122 | void onWindowInfosChanged(const gui::WindowInfosUpdate&) override; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 123 | void onPointerControllerDestroyed(); |
| 124 | |
| 125 | // This lock is also used by PointerController. See PointerController::getLock(). |
| 126 | std::mutex mLock; |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 127 | |
| 128 | private: |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 129 | PointerController* mPointerController GUARDED_BY(mLock); |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 130 | }; |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 131 | |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 132 | sp<DisplayInfoListener> mDisplayInfoListener; |
Linnan Li | 37c1b99 | 2023-11-24 13:05:13 +0800 | [diff] [blame] | 133 | const WindowListenerUnregisterConsumer mUnregisterWindowInfosListener; |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 134 | |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 135 | const ui::Transform& getTransformForDisplayLocked(ui::LogicalDisplayId displayId) const |
| 136 | REQUIRES(getLock()); |
Prabir Pradhan | f97fac3 | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 137 | |
Prabir Pradhan | 5693cee | 2021-12-31 06:51:15 -0800 | [diff] [blame] | 138 | void clearSpotsLocked() REQUIRES(getLock()); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 141 | class MousePointerController : public PointerController { |
| 142 | public: |
| 143 | /** A version of PointerController that controls one mouse pointer. */ |
| 144 | MousePointerController(const sp<PointerControllerPolicyInterface>& policy, |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 145 | const sp<Looper>& looper, SpriteController& spriteController); |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 146 | |
Prabir Pradhan | b0e2807 | 2023-11-08 21:09:34 +0000 | [diff] [blame] | 147 | ~MousePointerController() override; |
| 148 | |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 149 | void setPresentation(Presentation) override { |
| 150 | LOG_ALWAYS_FATAL("Should not be called"); |
| 151 | } |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 152 | void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override { |
Byoungho Jung | 6a2ce94 | 2023-10-07 16:19:19 +0900 | [diff] [blame] | 153 | LOG_ALWAYS_FATAL("Should not be called"); |
| 154 | } |
| 155 | void clearSpots() override { |
| 156 | LOG_ALWAYS_FATAL("Should not be called"); |
| 157 | } |
| 158 | }; |
| 159 | |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 160 | class TouchPointerController : public PointerController { |
| 161 | public: |
| 162 | /** A version of PointerController that controls touch spots. */ |
| 163 | TouchPointerController(const sp<PointerControllerPolicyInterface>& policy, |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 164 | const sp<Looper>& looper, SpriteController& spriteController); |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 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 | void move(float, float) override { |
| 169 | LOG_ALWAYS_FATAL("Should not be called"); |
| 170 | } |
| 171 | void setPosition(float, float) override { |
| 172 | LOG_ALWAYS_FATAL("Should not be called"); |
| 173 | } |
| 174 | FloatPoint getPosition() const override { |
| 175 | LOG_ALWAYS_FATAL("Should not be called"); |
| 176 | } |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 177 | ui::LogicalDisplayId getDisplayId() const override { |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 178 | LOG_ALWAYS_FATAL("Should not be called"); |
| 179 | } |
| 180 | void fade(Transition) override { |
| 181 | LOG_ALWAYS_FATAL("Should not be called"); |
| 182 | } |
| 183 | void unfade(Transition) override { |
| 184 | LOG_ALWAYS_FATAL("Should not be called"); |
| 185 | } |
| 186 | void setDisplayViewport(const DisplayViewport&) override { |
| 187 | LOG_ALWAYS_FATAL("Should not be called"); |
| 188 | } |
| 189 | void setPresentation(Presentation) override { |
| 190 | LOG_ALWAYS_FATAL("Should not be called"); |
| 191 | } |
Byoungho Jung | 1a1571d | 2023-11-03 20:12:50 +0900 | [diff] [blame] | 192 | void updatePointerIcon(PointerIconStyle) override { |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 193 | LOG_ALWAYS_FATAL("Should not be called"); |
| 194 | } |
Byoungho Jung | 1a1571d | 2023-11-03 20:12:50 +0900 | [diff] [blame] | 195 | void setCustomPointerIcon(const SpriteIcon&) override { |
Byoungho Jung | 51282e8 | 2023-10-27 18:15:41 +0900 | [diff] [blame] | 196 | LOG_ALWAYS_FATAL("Should not be called"); |
| 197 | } |
| 198 | // fade() should not be called by inactivity timeout. Do nothing. |
| 199 | void setInactivityTimeout(InactivityTimeout) override {} |
| 200 | }; |
| 201 | |
Byoungho Jung | 08daa25 | 2023-10-27 20:55:18 +0900 | [diff] [blame] | 202 | class StylusPointerController : public PointerController { |
| 203 | public: |
| 204 | /** A version of PointerController that controls one stylus pointer. */ |
| 205 | StylusPointerController(const sp<PointerControllerPolicyInterface>& policy, |
Prabir Pradhan | 7dff142 | 2024-05-03 23:33:28 +0000 | [diff] [blame] | 206 | const sp<Looper>& looper, SpriteController& spriteController); |
Byoungho Jung | 08daa25 | 2023-10-27 20:55:18 +0900 | [diff] [blame] | 207 | |
Prabir Pradhan | b0e2807 | 2023-11-08 21:09:34 +0000 | [diff] [blame] | 208 | ~StylusPointerController() override; |
| 209 | |
Byoungho Jung | 08daa25 | 2023-10-27 20:55:18 +0900 | [diff] [blame] | 210 | void setPresentation(Presentation) override { |
| 211 | LOG_ALWAYS_FATAL("Should not be called"); |
| 212 | } |
Linnan Li | 0defadf | 2024-05-05 19:17:05 +0800 | [diff] [blame] | 213 | void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override { |
Byoungho Jung | 08daa25 | 2023-10-27 20:55:18 +0900 | [diff] [blame] | 214 | LOG_ALWAYS_FATAL("Should not be called"); |
| 215 | } |
| 216 | void clearSpots() override { |
| 217 | LOG_ALWAYS_FATAL("Should not be called"); |
| 218 | } |
| 219 | }; |
| 220 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 221 | } // namespace android |
| 222 | |
| 223 | #endif // _UI_POINTER_CONTROLLER_H |