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 20 #include <PointerControllerInterface.h> 21 #include <gui/DisplayEventReceiver.h> 22 #include <gui/WindowInfosUpdate.h> 23 #include <input/DisplayViewport.h> 24 #include <input/Input.h> 25 #include <utils/BitSet.h> 26 #include <utils/Looper.h> 27 #include <utils/RefBase.h> 28 29 #include <map> 30 #include <memory> 31 #include <string> 32 #include <vector> 33 34 #include "MouseCursorController.h" 35 #include "PointerControllerContext.h" 36 #include "SpriteController.h" 37 #include "TouchSpotController.h" 38 39 namespace android { 40 41 /* 42 * Tracks pointer movements and draws the pointer sprite to a surface. 43 * 44 * Handles pointer acceleration and animation. 45 */ 46 class PointerController : public PointerControllerInterface { 47 public: 48 static std::shared_ptr<PointerController> create( 49 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, 50 SpriteController& spriteController, ControllerType type); 51 52 ~PointerController() override; 53 54 std::optional<FloatRect> getBounds() const override; 55 void move(float deltaX, float deltaY) override; 56 void setPosition(float x, float y) override; 57 FloatPoint getPosition() const override; 58 ui::LogicalDisplayId getDisplayId() const override; 59 void fade(Transition transition) override; 60 void unfade(Transition transition) override; 61 void setDisplayViewport(const DisplayViewport& viewport) override; 62 63 void setPresentation(Presentation presentation) override; 64 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, 65 BitSet32 spotIdBits, ui::LogicalDisplayId displayId) override; 66 void clearSpots() override; 67 void updatePointerIcon(PointerIconStyle iconId) override; 68 void setCustomPointerIcon(const SpriteIcon& icon) override; 69 void setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) override; 70 void clearSkipScreenshotFlags() override; 71 72 virtual void setInactivityTimeout(InactivityTimeout inactivityTimeout); 73 void doInactivityTimeout(); 74 void reloadPointerResources(); 75 void onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports); 76 77 void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos) 78 REQUIRES(getLock()); 79 80 std::string dump() override; 81 82 protected: 83 using WindowListenerRegisterConsumer = std::function<std::vector<gui::DisplayInfo>( 84 const sp<android::gui::WindowInfosListener>&)>; 85 using WindowListenerUnregisterConsumer = 86 std::function<void(const sp<android::gui::WindowInfosListener>&)>; 87 88 // Constructor used to test WindowInfosListener registration. 89 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, 90 SpriteController& spriteController, 91 const WindowListenerRegisterConsumer& registerListener, 92 WindowListenerUnregisterConsumer unregisterListener); 93 94 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper, 95 SpriteController& spriteController); 96 97 private: 98 friend PointerControllerContext::LooperCallback; 99 friend PointerControllerContext::MessageHandler; 100 101 // PointerController's DisplayInfoListener can outlive the PointerController because when the 102 // listener is registered, a strong pointer to the listener (which can extend its lifecycle) 103 // is given away. To avoid the small overhead of using two separate locks in these two objects, 104 // we use the DisplayInfoListener's lock in PointerController. 105 std::mutex& getLock() const; 106 107 PointerControllerContext mContext; 108 109 MouseCursorController mCursorController; 110 111 struct Locked { 112 Presentation presentation; 113 ui::LogicalDisplayId pointerDisplayId = ui::LogicalDisplayId::INVALID; 114 115 std::vector<gui::DisplayInfo> mDisplayInfos; 116 std::unordered_map<ui::LogicalDisplayId, TouchSpotController> spotControllers; 117 std::unordered_set<ui::LogicalDisplayId> displaysToSkipScreenshot; 118 } mLocked GUARDED_BY(getLock()); 119 120 class DisplayInfoListener : public gui::WindowInfosListener { 121 public: DisplayInfoListener(PointerController * pc)122 explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){}; 123 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override; 124 void onPointerControllerDestroyed(); 125 126 // This lock is also used by PointerController. See PointerController::getLock(). 127 std::mutex mLock; 128 129 private: 130 PointerController* mPointerController GUARDED_BY(mLock); 131 }; 132 133 sp<DisplayInfoListener> mDisplayInfoListener; 134 const WindowListenerUnregisterConsumer mUnregisterWindowInfosListener; 135 136 const ui::Transform& getTransformForDisplayLocked(ui::LogicalDisplayId displayId) const 137 REQUIRES(getLock()); 138 139 void clearSpotsLocked() REQUIRES(getLock()); 140 }; 141 142 class MousePointerController : public PointerController { 143 public: 144 /** A version of PointerController that controls one mouse pointer. */ 145 MousePointerController(const sp<PointerControllerPolicyInterface>& policy, 146 const sp<Looper>& looper, SpriteController& spriteController); 147 148 ~MousePointerController() override; 149 setPresentation(Presentation)150 void setPresentation(Presentation) override { 151 LOG_ALWAYS_FATAL("Should not be called"); 152 } setSpots(const PointerCoords *,const uint32_t *,BitSet32,ui::LogicalDisplayId)153 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override { 154 LOG_ALWAYS_FATAL("Should not be called"); 155 } clearSpots()156 void clearSpots() override { 157 LOG_ALWAYS_FATAL("Should not be called"); 158 } 159 }; 160 161 class TouchPointerController : public PointerController { 162 public: 163 /** A version of PointerController that controls touch spots. */ 164 TouchPointerController(const sp<PointerControllerPolicyInterface>& policy, 165 const sp<Looper>& looper, SpriteController& spriteController); 166 167 ~TouchPointerController() override; 168 getBounds()169 std::optional<FloatRect> getBounds() const override { 170 LOG_ALWAYS_FATAL("Should not be called"); 171 } move(float,float)172 void move(float, float) override { 173 LOG_ALWAYS_FATAL("Should not be called"); 174 } setPosition(float,float)175 void setPosition(float, float) override { 176 LOG_ALWAYS_FATAL("Should not be called"); 177 } getPosition()178 FloatPoint getPosition() const override { 179 LOG_ALWAYS_FATAL("Should not be called"); 180 } getDisplayId()181 ui::LogicalDisplayId getDisplayId() const override { 182 LOG_ALWAYS_FATAL("Should not be called"); 183 } fade(Transition)184 void fade(Transition) override { 185 LOG_ALWAYS_FATAL("Should not be called"); 186 } unfade(Transition)187 void unfade(Transition) override { 188 LOG_ALWAYS_FATAL("Should not be called"); 189 } setDisplayViewport(const DisplayViewport &)190 void setDisplayViewport(const DisplayViewport&) override { 191 LOG_ALWAYS_FATAL("Should not be called"); 192 } setPresentation(Presentation)193 void setPresentation(Presentation) override { 194 LOG_ALWAYS_FATAL("Should not be called"); 195 } updatePointerIcon(PointerIconStyle)196 void updatePointerIcon(PointerIconStyle) override { 197 LOG_ALWAYS_FATAL("Should not be called"); 198 } setCustomPointerIcon(const SpriteIcon &)199 void setCustomPointerIcon(const SpriteIcon&) override { 200 LOG_ALWAYS_FATAL("Should not be called"); 201 } 202 // fade() should not be called by inactivity timeout. Do nothing. setInactivityTimeout(InactivityTimeout)203 void setInactivityTimeout(InactivityTimeout) override {} 204 }; 205 206 class StylusPointerController : public PointerController { 207 public: 208 /** A version of PointerController that controls one stylus pointer. */ 209 StylusPointerController(const sp<PointerControllerPolicyInterface>& policy, 210 const sp<Looper>& looper, SpriteController& spriteController); 211 212 ~StylusPointerController() override; 213 setPresentation(Presentation)214 void setPresentation(Presentation) override { 215 LOG_ALWAYS_FATAL("Should not be called"); 216 } setSpots(const PointerCoords *,const uint32_t *,BitSet32,ui::LogicalDisplayId)217 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override { 218 LOG_ALWAYS_FATAL("Should not be called"); 219 } clearSpots()220 void clearSpots() override { 221 LOG_ALWAYS_FATAL("Should not be called"); 222 } 223 }; 224 225 } // namespace android 226 227 #endif // _UI_POINTER_CONTROLLER_H 228