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