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_MOUSE_CURSOR_CONTROLLER_H 18 #define _UI_MOUSE_CURSOR_CONTROLLER_H 19 20 #include <gui/DisplayEventReceiver.h> 21 #include <input/DisplayViewport.h> 22 #include <input/Input.h> 23 24 #include <functional> 25 #include <map> 26 #include <memory> 27 #include <vector> 28 29 #include "PointerControllerContext.h" 30 #include "SpriteController.h" 31 32 namespace android { 33 34 /* 35 * Helper class for PointerController that specifically handles 36 * mouse cursor resources and actions. 37 */ 38 class MouseCursorController { 39 public: 40 MouseCursorController(PointerControllerContext& context); 41 ~MouseCursorController(); 42 43 // Move the pointer and return unconsumed delta 44 vec2 move(vec2 delta); 45 void setPosition(vec2 position); 46 vec2 getPosition() const; 47 ui::LogicalDisplayId getDisplayId() const; 48 void fade(PointerControllerInterface::Transition transition); 49 void unfade(PointerControllerInterface::Transition transition); 50 void setDisplayViewport(const DisplayViewport& viewport, bool getAdditionalMouseResources); 51 void setStylusHoverMode(bool stylusHoverMode); 52 53 // Set/Unset flag to hide the mouse cursor on the mirrored display 54 void setSkipScreenshot(bool skip); 55 56 void updatePointerIcon(PointerIconStyle iconId); 57 void setCustomPointerIcon(const SpriteIcon& icon); 58 void reloadPointerResources(bool getAdditionalMouseResources); 59 60 void getAdditionalMouseResources(); 61 bool isViewportValid(); 62 63 bool doAnimations(nsecs_t timestamp); 64 65 bool resourcesLoaded(); 66 67 std::string dump() const; 68 69 private: 70 mutable std::mutex mLock; 71 72 PointerResources mResources; 73 74 PointerControllerContext& mContext; 75 76 struct Locked { 77 DisplayViewport viewport; 78 bool stylusHoverMode; 79 80 size_t animationFrameIndex; 81 nsecs_t lastFrameUpdatedTime; 82 83 int32_t pointerFadeDirection; 84 vec2 pointerPosition; 85 float pointerAlpha; 86 sp<Sprite> pointerSprite; 87 SpriteIcon pointerIcon; 88 bool updatePointerIcon; 89 90 bool resourcesLoaded; 91 92 std::map<PointerIconStyle, SpriteIcon> additionalMouseResources; 93 std::map<PointerIconStyle, PointerAnimation> animationResources; 94 95 PointerIconStyle requestedPointerType; 96 PointerIconStyle resolvedPointerType; 97 98 bool skipScreenshot{false}; 99 bool animating{false}; 100 101 } mLocked GUARDED_BY(mLock); 102 103 void setPositionLocked(vec2 position); 104 105 void updatePointerLocked(); 106 107 void loadResourcesLocked(bool getAdditionalMouseResources); 108 109 bool doBitmapAnimationLocked(nsecs_t timestamp); 110 bool doFadingAnimationLocked(nsecs_t timestamp); 111 112 void startAnimationLocked(); 113 FloatRect getBoundsLocked(); 114 }; 115 116 } // namespace android 117 118 #endif // _UI_MOUSE_CURSOR_CONTROLLER_H 119