blob: d76ca5d15a3150a49af1f8013b3950eda4dbaeab [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
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 Wrighta0bc6b12020-06-26 20:25:34 +010020#include <PointerControllerInterface.h>
21#include <gui/DisplayEventReceiver.h>
Patrick Williams8e47a672023-05-01 11:30:37 -050022#include <gui/WindowInfosUpdate.h>
Arthur Hungb9b32002018-12-18 17:39:43 +080023#include <input/DisplayViewport.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070024#include <input/Input.h>
Jeff Brown8a90e6e2012-05-11 12:24:35 -070025#include <utils/BitSet.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080026#include <utils/Looper.h>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010027#include <utils/RefBase.h>
28
29#include <map>
30#include <memory>
Michael Wright72a89132022-10-22 03:16:31 +010031#include <string>
Michael Wrighta0bc6b12020-06-26 20:25:34 +010032#include <vector>
33
Liam Harringtonc782be62020-07-17 19:48:24 +000034#include "MouseCursorController.h"
35#include "PointerControllerContext.h"
Michael Wrighta0bc6b12020-06-26 20:25:34 +010036#include "SpriteController.h"
Liam Harringtonc782be62020-07-17 19:48:24 +000037#include "TouchSpotController.h"
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039namespace android {
40
Jeff Brown2352b972011-04-12 22:39:53 -070041/*
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042 * Tracks pointer movements and draws the pointer sprite to a surface.
43 *
44 * Handles pointer acceleration and animation.
45 */
Michael Wrighta0bc6b12020-06-26 20:25:34 +010046class PointerController : public PointerControllerInterface {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047public:
Michael Wrighta0bc6b12020-06-26 20:25:34 +010048 static std::shared_ptr<PointerController> create(
49 const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Byoungho Jung6a2ce942023-10-07 16:19:19 +090050 SpriteController& spriteController, bool enabled,
51 ControllerType type = ControllerType::LEGACY);
Jeff Brown05dc66a2011-03-02 14:41:58 -080052
Prabir Pradhanf97fac32021-11-18 16:40:34 +000053 ~PointerController() override;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080054
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070055 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 Brownb4ff35d2011-01-02 16:37:43 -080063
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070064 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;
Byoungho Jung1a1571d2023-11-03 20:12:50 +090068 void updatePointerIcon(PointerIconStyle iconId) override;
69 void setCustomPointerIcon(const SpriteIcon& icon) override;
Arpit Singh80fd68a2024-03-26 18:41:06 +000070 void setSkipScreenshot(int32_t displayId, bool skip) override;
Jeff Brown2352b972011-04-12 22:39:53 -070071
Byoungho Jung51282e82023-10-27 18:15:41 +090072 virtual void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Liam Harringtonc782be62020-07-17 19:48:24 +000073 void doInactivityTimeout();
Jun Mukai19a56012015-11-24 11:25:52 -080074 void reloadPointerResources();
Prabir Pradhanc2200182023-06-09 23:39:15 +000075 void onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080076
Prabir Pradhan5693cee2021-12-31 06:51:15 -080077 void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos)
78 REQUIRES(getLock());
79
Byoungho Jung6a2ce942023-10-07 16:19:19 +090080 std::string dump() override;
Michael Wright72a89132022-10-22 03:16:31 +010081
Prabir Pradhan5693cee2021-12-31 06:51:15 -080082protected:
Prabir Pradhanbdf93692024-01-23 18:08:28 +000083 using WindowListenerRegisterConsumer = std::function<std::vector<gui::DisplayInfo>(
84 const sp<android::gui::WindowInfosListener>&)>;
Linnan Li37c1b992023-11-24 13:05:13 +080085 using WindowListenerUnregisterConsumer =
Prabir Pradhan5693cee2021-12-31 06:51:15 -080086 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,
Prabir Pradhan3401d232023-06-15 23:15:32 +000090 SpriteController& spriteController, bool enabled,
Linnan Li37c1b992023-11-24 13:05:13 +080091 const WindowListenerRegisterConsumer& registerListener,
92 WindowListenerUnregisterConsumer unregisterListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +000093
Prabir Pradhan5693cee2021-12-31 06:51:15 -080094 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Prabir Pradhan3401d232023-06-15 23:15:32 +000095 SpriteController& spriteController, bool enabled);
Prabir Pradhan5693cee2021-12-31 06:51:15 -080096
Byoungho Jung6a2ce942023-10-07 16:19:19 +090097private:
Liam Harringtonc782be62020-07-17 19:48:24 +000098 friend PointerControllerContext::LooperCallback;
99 friend PointerControllerContext::MessageHandler;
Jeff Brown2352b972011-04-12 22:39:53 -0700100
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800101 // 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;
Jeff Brown2352b972011-04-12 22:39:53 -0700106
Prabir Pradhan3401d232023-06-15 23:15:32 +0000107 const bool mEnabled;
108
Liam Harringtonc782be62020-07-17 19:48:24 +0000109 PointerControllerContext mContext;
Jeff Brown2352b972011-04-12 22:39:53 -0700110
Liam Harringtonc782be62020-07-17 19:48:24 +0000111 MouseCursorController mCursorController;
Jeff Brown2352b972011-04-12 22:39:53 -0700112
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800113 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700114 Presentation presentation;
Prabir Pradhan0e3d6652022-03-10 14:39:46 +0000115 int32_t pointerDisplayId = ADISPLAY_ID_NONE;
Jeff Brown2352b972011-04-12 22:39:53 -0700116
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000117 std::vector<gui::DisplayInfo> mDisplayInfos;
Liam Harringtonc782be62020-07-17 19:48:24 +0000118 std::unordered_map<int32_t /* displayId */, TouchSpotController> spotControllers;
Arpit Singh80fd68a2024-03-26 18:41:06 +0000119 std::unordered_set<int32_t /* displayId */> displaysToSkipScreenshot;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800120 } mLocked GUARDED_BY(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800121
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000122 class DisplayInfoListener : public gui::WindowInfosListener {
123 public:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800124 explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
Patrick Williams8e47a672023-05-01 11:30:37 -0500125 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800126 void onPointerControllerDestroyed();
127
128 // This lock is also used by PointerController. See PointerController::getLock().
129 std::mutex mLock;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000130
131 private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800132 PointerController* mPointerController GUARDED_BY(mLock);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000133 };
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800134
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000135 sp<DisplayInfoListener> mDisplayInfoListener;
Linnan Li37c1b992023-11-24 13:05:13 +0800136 const WindowListenerUnregisterConsumer mUnregisterWindowInfosListener;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000137
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800138 const ui::Transform& getTransformForDisplayLocked(int displayId) const REQUIRES(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000139
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800140 void clearSpotsLocked() REQUIRES(getLock());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800141};
142
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900143class MousePointerController : public PointerController {
144public:
145 /** A version of PointerController that controls one mouse pointer. */
146 MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
147 const sp<Looper>& looper, SpriteController& spriteController,
148 bool enabled);
149
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000150 ~MousePointerController() override;
151
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900152 void setPresentation(Presentation) override {
153 LOG_ALWAYS_FATAL("Should not be called");
154 }
155 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, int32_t) override {
156 LOG_ALWAYS_FATAL("Should not be called");
157 }
158 void clearSpots() override {
159 LOG_ALWAYS_FATAL("Should not be called");
160 }
161};
162
Byoungho Jung51282e82023-10-27 18:15:41 +0900163class TouchPointerController : public PointerController {
164public:
165 /** A version of PointerController that controls touch spots. */
166 TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
167 const sp<Looper>& looper, SpriteController& spriteController,
168 bool enabled);
169
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000170 ~TouchPointerController() override;
171
Byoungho Jung51282e82023-10-27 18:15:41 +0900172 std::optional<FloatRect> getBounds() const override {
173 LOG_ALWAYS_FATAL("Should not be called");
174 }
175 void move(float, float) override {
176 LOG_ALWAYS_FATAL("Should not be called");
177 }
178 void setPosition(float, float) override {
179 LOG_ALWAYS_FATAL("Should not be called");
180 }
181 FloatPoint getPosition() const override {
182 LOG_ALWAYS_FATAL("Should not be called");
183 }
184 int32_t getDisplayId() const override {
185 LOG_ALWAYS_FATAL("Should not be called");
186 }
187 void fade(Transition) override {
188 LOG_ALWAYS_FATAL("Should not be called");
189 }
190 void unfade(Transition) override {
191 LOG_ALWAYS_FATAL("Should not be called");
192 }
193 void setDisplayViewport(const DisplayViewport&) override {
194 LOG_ALWAYS_FATAL("Should not be called");
195 }
196 void setPresentation(Presentation) override {
197 LOG_ALWAYS_FATAL("Should not be called");
198 }
Byoungho Jung1a1571d2023-11-03 20:12:50 +0900199 void updatePointerIcon(PointerIconStyle) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900200 LOG_ALWAYS_FATAL("Should not be called");
201 }
Byoungho Jung1a1571d2023-11-03 20:12:50 +0900202 void setCustomPointerIcon(const SpriteIcon&) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900203 LOG_ALWAYS_FATAL("Should not be called");
204 }
205 // fade() should not be called by inactivity timeout. Do nothing.
206 void setInactivityTimeout(InactivityTimeout) override {}
207};
208
Byoungho Jung08daa252023-10-27 20:55:18 +0900209class StylusPointerController : public PointerController {
210public:
211 /** A version of PointerController that controls one stylus pointer. */
212 StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
213 const sp<Looper>& looper, SpriteController& spriteController,
214 bool enabled);
215
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000216 ~StylusPointerController() override;
217
Byoungho Jung08daa252023-10-27 20:55:18 +0900218 void setPresentation(Presentation) override {
219 LOG_ALWAYS_FATAL("Should not be called");
220 }
221 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, int32_t) override {
222 LOG_ALWAYS_FATAL("Should not be called");
223 }
224 void clearSpots() override {
225 LOG_ALWAYS_FATAL("Should not be called");
226 }
227};
228
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800229} // namespace android
230
231#endif // _UI_POINTER_CONTROLLER_H