blob: afd7215c7fbae3f8c8682608d408a39573ded644 [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
Arpit Singha09a94d2024-11-26 15:50:17 +000054 vec2 move(float deltaX, float deltaY) override;
Siarhei Vishniakoua21a5492023-03-15 09:29:53 -070055 void setPosition(float x, float y) override;
Arpit Singha09a94d2024-11-26 15:50:17 +000056 vec2 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;
Arpit Singh1886eea2024-11-18 20:42:54 +000070 ui::Transform getDisplayTransform() const 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 Pradhan7dff1422024-05-03 23:33:28 +000090 SpriteController& spriteController,
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 Pradhan7dff1422024-05-03 23:33:28 +000095 SpriteController& spriteController);
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
Liam Harringtonc782be62020-07-17 19:48:24 +0000107 PointerControllerContext mContext;
Jeff Brown2352b972011-04-12 22:39:53 -0700108
Liam Harringtonc782be62020-07-17 19:48:24 +0000109 MouseCursorController mCursorController;
Jeff Brown2352b972011-04-12 22:39:53 -0700110
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800111 struct Locked {
Jeff Brown2352b972011-04-12 22:39:53 -0700112 Presentation presentation;
Siarhei Vishniakou445af182024-05-13 13:17:05 -0700113 ui::LogicalDisplayId pointerDisplayId = ui::LogicalDisplayId::INVALID;
Jeff Brown2352b972011-04-12 22:39:53 -0700114
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000115 std::vector<gui::DisplayInfo> mDisplayInfos;
Linnan Li0defadf2024-05-05 19:17:05 +0800116 std::unordered_map<ui::LogicalDisplayId, TouchSpotController> spotControllers;
117 std::unordered_set<ui::LogicalDisplayId> displaysToSkipScreenshot;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800118 } mLocked GUARDED_BY(getLock());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800119
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000120 class DisplayInfoListener : public gui::WindowInfosListener {
121 public:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800122 explicit DisplayInfoListener(PointerController* pc) : mPointerController(pc){};
Patrick Williams8e47a672023-05-01 11:30:37 -0500123 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override;
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800124 void onPointerControllerDestroyed();
125
126 // This lock is also used by PointerController. See PointerController::getLock().
127 std::mutex mLock;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000128
129 private:
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800130 PointerController* mPointerController GUARDED_BY(mLock);
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000131 };
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800132
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000133 sp<DisplayInfoListener> mDisplayInfoListener;
Linnan Li37c1b992023-11-24 13:05:13 +0800134 const WindowListenerUnregisterConsumer mUnregisterWindowInfosListener;
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000135
Linnan Li0defadf2024-05-05 19:17:05 +0800136 const ui::Transform& getTransformForDisplayLocked(ui::LogicalDisplayId displayId) const
137 REQUIRES(getLock());
Prabir Pradhanf97fac32021-11-18 16:40:34 +0000138
Prabir Pradhan5693cee2021-12-31 06:51:15 -0800139 void clearSpotsLocked() REQUIRES(getLock());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800140};
141
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900142class MousePointerController : public PointerController {
143public:
144 /** A version of PointerController that controls one mouse pointer. */
145 MousePointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan7dff1422024-05-03 23:33:28 +0000146 const sp<Looper>& looper, SpriteController& spriteController);
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900147
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000148 ~MousePointerController() override;
149
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900150 void setPresentation(Presentation) override {
151 LOG_ALWAYS_FATAL("Should not be called");
152 }
Linnan Li0defadf2024-05-05 19:17:05 +0800153 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override {
Byoungho Jung6a2ce942023-10-07 16:19:19 +0900154 LOG_ALWAYS_FATAL("Should not be called");
155 }
156 void clearSpots() override {
157 LOG_ALWAYS_FATAL("Should not be called");
158 }
159};
160
Byoungho Jung51282e82023-10-27 18:15:41 +0900161class TouchPointerController : public PointerController {
162public:
163 /** A version of PointerController that controls touch spots. */
164 TouchPointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan7dff1422024-05-03 23:33:28 +0000165 const sp<Looper>& looper, SpriteController& spriteController);
Byoungho Jung51282e82023-10-27 18:15:41 +0900166
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000167 ~TouchPointerController() override;
168
Arpit Singha09a94d2024-11-26 15:50:17 +0000169 vec2 move(float, float) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900170 LOG_ALWAYS_FATAL("Should not be called");
171 }
172 void setPosition(float, float) override {
173 LOG_ALWAYS_FATAL("Should not be called");
174 }
Arpit Singha09a94d2024-11-26 15:50:17 +0000175 vec2 getPosition() const override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900176 LOG_ALWAYS_FATAL("Should not be called");
177 }
Linnan Li0defadf2024-05-05 19:17:05 +0800178 ui::LogicalDisplayId getDisplayId() const override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900179 LOG_ALWAYS_FATAL("Should not be called");
180 }
181 void fade(Transition) override {
182 LOG_ALWAYS_FATAL("Should not be called");
183 }
184 void unfade(Transition) override {
185 LOG_ALWAYS_FATAL("Should not be called");
186 }
187 void setDisplayViewport(const DisplayViewport&) override {
188 LOG_ALWAYS_FATAL("Should not be called");
189 }
190 void setPresentation(Presentation) override {
191 LOG_ALWAYS_FATAL("Should not be called");
192 }
Byoungho Jung1a1571d2023-11-03 20:12:50 +0900193 void updatePointerIcon(PointerIconStyle) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900194 LOG_ALWAYS_FATAL("Should not be called");
195 }
Byoungho Jung1a1571d2023-11-03 20:12:50 +0900196 void setCustomPointerIcon(const SpriteIcon&) override {
Byoungho Jung51282e82023-10-27 18:15:41 +0900197 LOG_ALWAYS_FATAL("Should not be called");
198 }
199 // fade() should not be called by inactivity timeout. Do nothing.
200 void setInactivityTimeout(InactivityTimeout) override {}
201};
202
Byoungho Jung08daa252023-10-27 20:55:18 +0900203class StylusPointerController : public PointerController {
204public:
205 /** A version of PointerController that controls one stylus pointer. */
206 StylusPointerController(const sp<PointerControllerPolicyInterface>& policy,
Prabir Pradhan7dff1422024-05-03 23:33:28 +0000207 const sp<Looper>& looper, SpriteController& spriteController);
Byoungho Jung08daa252023-10-27 20:55:18 +0900208
Prabir Pradhanb0e28072023-11-08 21:09:34 +0000209 ~StylusPointerController() override;
210
Byoungho Jung08daa252023-10-27 20:55:18 +0900211 void setPresentation(Presentation) override {
212 LOG_ALWAYS_FATAL("Should not be called");
213 }
Linnan Li0defadf2024-05-05 19:17:05 +0800214 void setSpots(const PointerCoords*, const uint32_t*, BitSet32, ui::LogicalDisplayId) override {
Byoungho Jung08daa252023-10-27 20:55:18 +0900215 LOG_ALWAYS_FATAL("Should not be called");
216 }
217 void clearSpots() override {
218 LOG_ALWAYS_FATAL("Should not be called");
219 }
220};
221
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800222} // namespace android
223
224#endif // _UI_POINTER_CONTROLLER_H