blob: ee8d1211341fa144d21178af702faeb2d9c5d5fc [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,
Prabir Pradhan7dff1422024-05-03 23:33:28 +000050 SpriteController& spriteController, ControllerType type);
Jeff Brown05dc66a2011-03-02 14:41:58 -080051
Prabir Pradhanf97fac32021-11-18 16:40:34 +000052 ~PointerController() override;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080053
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070054 void move(float deltaX, float deltaY) override;
55 void setPosition(float x, float y) override;
56 FloatPoint getPosition() const override;
Linnan Li0defadf2024-05-05 19:17:05 +080057 ui::LogicalDisplayId getDisplayId() const override;
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070058 void fade(Transition transition) override;
59 void unfade(Transition transition) override;
60 void setDisplayViewport(const DisplayViewport& viewport) override;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080061
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070062 void setPresentation(Presentation presentation) override;
63 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
Linnan Li0defadf2024-05-05 19:17:05 +080064 BitSet32 spotIdBits, ui::LogicalDisplayId displayId) override;
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070065 void clearSpots() override;
Byoungho Jung1a1571d2023-11-03 20:12:50 +090066 void updatePointerIcon(PointerIconStyle iconId) override;
67 void setCustomPointerIcon(const SpriteIcon& icon) override;
Arpit Singhf4ae0ac2024-03-26 18:41:06 +000068 void setSkipScreenshotFlagForDisplay(ui::LogicalDisplayId displayId) override;
69 void clearSkipScreenshotFlags() override;
Jeff Brown2352b972011-04-12 22:39:53 -070070
Byoungho Jung51282e82023-10-27 18:15:41 +090071 virtual void setInactivityTimeout(InactivityTimeout inactivityTimeout);
Liam Harringtonc782be62020-07-17 19:48:24 +000072 void doInactivityTimeout();
Jun Mukai19a56012015-11-24 11:25:52 -080073 void reloadPointerResources();
Prabir Pradhanc2200182023-06-09 23:39:15 +000074 void onDisplayViewportsUpdated(const std::vector<DisplayViewport>& viewports);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080075
Prabir Pradhan5693cee2021-12-31 06:51:15 -080076 void onDisplayInfosChangedLocked(const std::vector<gui::DisplayInfo>& displayInfos)
77 REQUIRES(getLock());
78
Byoungho Jung6a2ce942023-10-07 16:19:19 +090079 std::string dump() override;
Michael Wright72a89132022-10-22 03:16:31 +010080
Prabir Pradhan5693cee2021-12-31 06:51:15 -080081protected:
Prabir Pradhanbdf93692024-01-23 18:08:28 +000082 using WindowListenerRegisterConsumer = std::function<std::vector<gui::DisplayInfo>(
83 const sp<android::gui::WindowInfosListener>&)>;
Linnan Li37c1b992023-11-24 13:05:13 +080084 using WindowListenerUnregisterConsumer =
Prabir Pradhan5693cee2021-12-31 06:51:15 -080085 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 Pradhan7dff1422024-05-03 23:33:28 +000089 SpriteController& spriteController,
Linnan Li37c1b992023-11-24 13:05:13 +080090 const WindowListenerRegisterConsumer& registerListener,
91 WindowListenerUnregisterConsumer unregisterListener);
Prabir Pradhanf97fac32021-11-18 16:40:34 +000092
Prabir Pradhan5693cee2021-12-31 06:51:15 -080093 PointerController(const sp<PointerControllerPolicyInterface>& policy, const sp<Looper>& looper,
Prabir Pradhan7dff1422024-05-03 23:33:28 +000094 SpriteController& spriteController);
Prabir Pradhan5693cee2021-12-31 06:51:15 -080095
Byoungho Jung6a2ce942023-10-07 16:19:19 +090096private:
Liam Harringtonc782be62020-07-17 19:48:24 +000097 friend PointerControllerContext::LooperCallback;
98 friend PointerControllerContext::MessageHandler;
Jeff Brown2352b972011-04-12 22:39:53 -070099
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800100 // 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 Brown2352b972011-04-12 22:39:53 -0700105
Liam Harringtonc782be62020-07-17 19:48:24 +0000106 PointerControllerContext mContext;
Jeff Brown2352b972011-04-12 22:39:53 -0700107
Liam Harringtonc782be62020-07-17 19:48:24 +0000108 MouseCursorController mCursorController;
Jeff Brown2352b972011-04-12 22:39:53 -0700109
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800110 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700111 Presentation presentation;
Siarhei Vishniakou445af182024-05-13 13:17:05 -0700112 ui::LogicalDisplayId pointerDisplayId = ui::LogicalDisplayId::INVALID;
Jeff Brown2352b972011-04-12 22:39:53 -0700113
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000114 std::vector<gui::DisplayInfo> mDisplayInfos;
Linnan Li0defadf2024-05-05 19:17:05 +0800115 std::unordered_map<ui::LogicalDisplayId, TouchSpotController> spotControllers;
116 std::unordered_set<ui::LogicalDisplayId> displaysToSkipScreenshot;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800117 } mLocked GUARDED_BY(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800118
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000119 class DisplayInfoListener : public gui::WindowInfosListener {
120 public:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800121 explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
Patrick Williams8e47a672023-05-01 11:30:37 -0500122 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800123 void onPointerControllerDestroyed();
124
125 // This lock is also used by PointerController. See PointerController::getLock().
126 std::mutex mLock;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000127
128 private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800129 PointerController* mPointerController GUARDED_BY(mLock);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000130 };
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800131
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000132 sp<DisplayInfoListener> mDisplayInfoListener;
Linnan Li37c1b992023-11-24 13:05:13 +0800133 const WindowListenerUnregisterConsumer mUnregisterWindowInfosListener;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000134
Linnan Li0defadf2024-05-05 19:17:05 +0800135 const ui::Transform& getTransformForDisplayLocked(ui::LogicalDisplayId displayId) const
136 REQUIRES(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000137
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800138 void clearSpotsLocked() REQUIRES(getLock());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800139};
140
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900141class MousePointerController : public PointerController {
142public:
143 /** A version of PointerController that controls one mouse pointer. */
144 MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan7dff1422024-05-03 23:33:28 +0000145 const sp<Looper>& looper, SpriteController& spriteController);
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900146
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000147 ~MousePointerController() override;
148
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900149 void setPresentation(Presentation) override {
150 LOG_ALWAYS_FATAL("Should not be called");
151 }
Linnan Li0defadf2024-05-05 19:17:05 +0800152 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override {
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900153 LOG_ALWAYS_FATAL("Should not be called");
154 }
155 void clearSpots() override {
156 LOG_ALWAYS_FATAL("Should not be called");
157 }
158};
159
Byoungho Jung51282e82023-10-27 18:15:41 +0900160class TouchPointerController : public PointerController {
161public:
162 /** A version of PointerController that controls touch spots. */
163 TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan7dff1422024-05-03 23:33:28 +0000164 const sp<Looper>& looper, SpriteController& spriteController);
Byoungho Jung51282e82023-10-27 18:15:41 +0900165
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000166 ~TouchPointerController() override;
167
Byoungho Jung51282e82023-10-27 18:15:41 +0900168 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 Li0defadf2024-05-05 19:17:05 +0800177 ui::LogicalDisplayId getDisplayId() const override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900178 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 Jung1a1571d2023-11-03 20:12:50 +0900192 void updatePointerIcon(PointerIconStyle) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900193 LOG_ALWAYS_FATAL("Should not be called");
194 }
Byoungho Jung1a1571d2023-11-03 20:12:50 +0900195 void setCustomPointerIcon(const SpriteIcon&) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900196 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 Jung08daa252023-10-27 20:55:18 +0900202class StylusPointerController : public PointerController {
203public:
204 /** A version of PointerController that controls one stylus pointer. */
205 StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan7dff1422024-05-03 23:33:28 +0000206 const sp<Looper>& looper, SpriteController& spriteController);
Byoungho Jung08daa252023-10-27 20:55:18 +0900207
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000208 ~StylusPointerController() override;
209
Byoungho Jung08daa252023-10-27 20:55:18 +0900210 void setPresentation(Presentation) override {
211 LOG_ALWAYS_FATAL("Should not be called");
212 }
Linnan Li0defadf2024-05-05 19:17:05 +0800213 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override {
Byoungho Jung08daa252023-10-27 20:55:18 +0900214 LOG_ALWAYS_FATAL("Should not be called");
215 }
216 void clearSpots() override {
217 LOG_ALWAYS_FATAL("Should not be called");
218 }
219};
220
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800221} // namespace android
222
223#endif // _UI_POINTER_CONTROLLER_H