blob: 2434d2badb6f7245cdd7865b2adf8fd6663bdf09 [file] [log] [blame]
Prabir Pradhanb56e92c2023-06-09 23:40:37 +00001/*
2 * Copyright 2023 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#define LOG_TAG "PointerChoreographer"
18
Byoungho Jungda10dd32023-10-06 17:03:45 +090019#include <android-base/logging.h>
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000020#include <com_android_input_flags.h>
21#if defined(__ANDROID__)
22#include <gui/SurfaceComposerClient.h>
23#endif
Arpit Singhb65e2bd2024-06-03 09:48:16 +000024#include <input/Keyboard.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090025#include <input/PrintTools.h>
Arpit Singh4b6ad2d2024-04-04 11:54:20 +000026#include <unordered_set>
Byoungho Jungda10dd32023-10-06 17:03:45 +090027
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000028#include "PointerChoreographer.h"
29
Byoungho Jungda10dd32023-10-06 17:03:45 +090030#define INDENT " "
31
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000032namespace android {
33
Byoungho Jungda10dd32023-10-06 17:03:45 +090034namespace {
Prabir Pradhan5a51a222024-03-05 03:54:00 +000035
Byoungho Jungda10dd32023-10-06 17:03:45 +090036bool isFromMouse(const NotifyMotionArgs& args) {
37 return isFromSource(args.source, AINPUT_SOURCE_MOUSE) &&
38 args.pointerProperties[0].toolType == ToolType::MOUSE;
39}
40
Byoungho Jungee6268f2023-10-30 17:27:26 +090041bool isFromTouchpad(const NotifyMotionArgs& args) {
42 return isFromSource(args.source, AINPUT_SOURCE_MOUSE) &&
43 args.pointerProperties[0].toolType == ToolType::FINGER;
44}
45
Prabir Pradhan4c977a42024-03-15 16:47:37 +000046bool isFromDrawingTablet(const NotifyMotionArgs& args) {
47 return isFromSource(args.source, AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS) &&
48 isStylusToolType(args.pointerProperties[0].toolType);
49}
50
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090051bool isHoverAction(int32_t action) {
52 return action == AMOTION_EVENT_ACTION_HOVER_ENTER ||
53 action == AMOTION_EVENT_ACTION_HOVER_MOVE || action == AMOTION_EVENT_ACTION_HOVER_EXIT;
54}
55
56bool isStylusHoverEvent(const NotifyMotionArgs& args) {
57 return isStylusEvent(args.source, args.pointerProperties) && isHoverAction(args.action);
58}
Prabir Pradhan5a51a222024-03-05 03:54:00 +000059
Prabir Pradhan4c977a42024-03-15 16:47:37 +000060bool isMouseOrTouchpad(uint32_t sources) {
61 // Check if this is a mouse or touchpad, but not a drawing tablet.
62 return isFromSource(sources, AINPUT_SOURCE_MOUSE_RELATIVE) ||
63 (isFromSource(sources, AINPUT_SOURCE_MOUSE) &&
64 !isFromSource(sources, AINPUT_SOURCE_STYLUS));
65}
66
Linnan Li13bf76a2024-05-05 19:18:02 +080067inline void notifyPointerDisplayChange(
68 std::optional<std::tuple<ui::LogicalDisplayId, FloatPoint>> change,
69 PointerChoreographerPolicyInterface& policy) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +000070 if (!change) {
71 return;
72 }
73 const auto& [displayId, cursorPosition] = *change;
74 policy.notifyPointerDisplayIdChanged(displayId, cursorPosition);
75}
76
Prabir Pradhan4c977a42024-03-15 16:47:37 +000077void setIconForController(const std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle>& icon,
78 PointerControllerInterface& controller) {
79 if (std::holds_alternative<std::unique_ptr<SpriteIcon>>(icon)) {
80 if (std::get<std::unique_ptr<SpriteIcon>>(icon) == nullptr) {
81 LOG(FATAL) << "SpriteIcon should not be null";
82 }
83 controller.setCustomPointerIcon(*std::get<std::unique_ptr<SpriteIcon>>(icon));
84 } else {
85 controller.updatePointerIcon(std::get<PointerIconStyle>(icon));
86 }
87}
88
Arpit Singh420d0742024-04-04 11:54:20 +000089// filters and returns a set of privacy sensitive displays that are currently visible.
90std::unordered_set<ui::LogicalDisplayId> getPrivacySensitiveDisplaysFromWindowInfos(
91 const std::vector<gui::WindowInfo>& windowInfos) {
92 std::unordered_set<ui::LogicalDisplayId> privacySensitiveDisplays;
93 for (const auto& windowInfo : windowInfos) {
94 if (!windowInfo.inputConfig.test(gui::WindowInfo::InputConfig::NOT_VISIBLE) &&
95 windowInfo.inputConfig.test(gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY)) {
96 privacySensitiveDisplays.insert(windowInfo.displayId);
97 }
98 }
99 return privacySensitiveDisplays;
100}
101
Byoungho Jungda10dd32023-10-06 17:03:45 +0900102} // namespace
103
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000104// --- PointerChoreographer ---
105
Arpit Singhbd49b282024-05-23 18:02:54 +0000106PointerChoreographer::PointerChoreographer(InputListenerInterface& inputListener,
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000107 PointerChoreographerPolicyInterface& policy)
Arpit Singhbd49b282024-05-23 18:02:54 +0000108 : PointerChoreographer(
109 inputListener, policy,
110 [](const sp<android::gui::WindowInfosListener>& listener) {
111 auto initialInfo = std::make_pair(std::vector<android::gui::WindowInfo>{},
112 std::vector<android::gui::DisplayInfo>{});
113#if defined(__ANDROID__)
114 SurfaceComposerClient::getDefault()->addWindowInfosListener(listener,
115 &initialInfo);
116#endif
117 return initialInfo.first;
118 },
119 [](const sp<android::gui::WindowInfosListener>& listener) {
120#if defined(__ANDROID__)
121 SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener);
122#endif
123 }) {
124}
125
126PointerChoreographer::PointerChoreographer(
127 android::InputListenerInterface& listener,
128 android::PointerChoreographerPolicyInterface& policy,
129 const android::PointerChoreographer::WindowListenerRegisterConsumer& registerListener,
130 const android::PointerChoreographer::WindowListenerUnregisterConsumer& unregisterListener)
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000131 : mTouchControllerConstructor([this]() {
Prabir Pradhan16788792023-11-08 21:07:21 +0000132 return mPolicy.createPointerController(
133 PointerControllerInterface::ControllerType::TOUCH);
134 }),
135 mNextListener(listener),
Byoungho Jungda10dd32023-10-06 17:03:45 +0900136 mPolicy(policy),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700137 mDefaultMouseDisplayId(ui::LogicalDisplayId::DEFAULT),
138 mNotifiedPointerDisplayId(ui::LogicalDisplayId::INVALID),
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900139 mShowTouchesEnabled(false),
Arpit Singhbd49b282024-05-23 18:02:54 +0000140 mStylusPointerIconEnabled(false),
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000141 mCurrentFocusedDisplay(ui::LogicalDisplayId::DEFAULT),
Arpit Singh7918c822024-11-20 11:28:44 +0000142 mIsWindowInfoListenerRegistered(false),
143 mWindowInfoListener(sp<PointerChoreographerDisplayInfoListener>::make(this)),
Arpit Singhbd49b282024-05-23 18:02:54 +0000144 mRegisterListener(registerListener),
145 mUnregisterListener(unregisterListener) {}
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000146
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000147PointerChoreographer::~PointerChoreographer() {
Arpit Singh7918c822024-11-20 11:28:44 +0000148 if (mIsWindowInfoListenerRegistered) {
149 mUnregisterListener(mWindowInfoListener);
150 mIsWindowInfoListenerRegistered = false;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000151 }
152 mWindowInfoListener->onPointerChoreographerDestroyed();
153}
154
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000155void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000156 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900157
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000158 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000159 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000160
161 mInputDeviceInfos = args.inputDeviceInfos;
162 pointerDisplayChange = updatePointerControllersLocked();
163 } // release lock
164
165 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000166 mNextListener.notify(args);
167}
168
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000169void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) {
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000170 fadeMouseCursorOnKeyPress(args);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000171 mNextListener.notify(args);
172}
173
174void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900175 NotifyMotionArgs newArgs = processMotion(args);
176
177 mNextListener.notify(newArgs);
178}
179
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000180void PointerChoreographer::fadeMouseCursorOnKeyPress(const android::NotifyKeyArgs& args) {
181 if (args.action == AKEY_EVENT_ACTION_UP || isMetaKey(args.keyCode)) {
182 return;
183 }
184 // Meta state for these keys is ignored for dismissing cursor while typing
185 constexpr static int32_t ALLOW_FADING_META_STATE_MASK = AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON |
186 AMETA_SCROLL_LOCK_ON | AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON | AMETA_SHIFT_ON;
187 if (args.metaState & ~ALLOW_FADING_META_STATE_MASK) {
188 // Do not fade if any other meta state is active
189 return;
190 }
191 if (!mPolicy.isInputMethodConnectionActive()) {
192 return;
193 }
194
Arpit Singh7918c822024-11-20 11:28:44 +0000195 std::scoped_lock _l(getLock());
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000196 ui::LogicalDisplayId targetDisplay = args.displayId;
197 if (targetDisplay == ui::LogicalDisplayId::INVALID) {
198 targetDisplay = mCurrentFocusedDisplay;
199 }
200 auto it = mMousePointersByDisplay.find(targetDisplay);
201 if (it != mMousePointersByDisplay.end()) {
Arpit Singh849beb42024-06-06 07:14:17 +0000202 mPolicy.notifyMouseCursorFadedOnTyping();
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000203 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
204 }
205}
206
Byoungho Jungda10dd32023-10-06 17:03:45 +0900207NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) {
Arpit Singhe33845c2024-10-25 20:59:24 +0000208 NotifyMotionArgs newArgs(args);
209 PointerDisplayChange pointerDisplayChange;
210 { // acquire lock
211 std::scoped_lock _l(getLock());
212 if (isFromMouse(args)) {
213 newArgs = processMouseEventLocked(args);
214 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
215 } else if (isFromTouchpad(args)) {
216 newArgs = processTouchpadEventLocked(args);
217 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
218 } else if (isFromDrawingTablet(args)) {
219 processDrawingTabletEventLocked(args);
220 } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) {
221 processStylusHoverEventLocked(args);
222 } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) {
223 processTouchscreenAndStylusEventLocked(args);
224 }
225 } // release lock
Byoungho Jungda10dd32023-10-06 17:03:45 +0900226
Arpit Singhe33845c2024-10-25 20:59:24 +0000227 if (pointerDisplayChange) {
228 // pointer display may have changed if mouse crossed display boundary
229 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900230 }
Arpit Singhe33845c2024-10-25 20:59:24 +0000231 return newArgs;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900232}
233
234NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) {
235 if (args.getPointerCount() != 1) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000236 LOG(FATAL) << "Only mouse events with a single pointer are currently supported: "
237 << args.dump();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900238 }
239
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000240 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000241 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900242 NotifyMotionArgs newArgs(args);
243 newArgs.displayId = displayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900244
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900245 if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) {
246 // This is an absolute mouse device that knows about the location of the cursor on the
247 // display, so set the cursor position to the specified location.
248 const auto [x, y] = pc.getPosition();
249 const float deltaX = args.xCursorPosition - x;
250 const float deltaY = args.yCursorPosition - y;
251 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
252 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
253 pc.setPosition(args.xCursorPosition, args.yCursorPosition);
254 } else {
255 // This is a relative mouse, so move the cursor by the specified amount.
Arpit Singh5cd74972024-11-25 11:17:42 +0000256 processPointerDeviceMotionEventLocked(/*byref*/ newArgs, /*byref*/ pc);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900257 }
Arpit Singhe33845c2024-10-25 20:59:24 +0000258 // Note displayId may have changed if the cursor moved to a different display
259 if (canUnfadeOnDisplay(newArgs.displayId)) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000260 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
261 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900262 return newArgs;
263}
264
Byoungho Jungee6268f2023-10-30 17:27:26 +0900265NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000266 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000267 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900268
269 NotifyMotionArgs newArgs(args);
270 newArgs.displayId = displayId;
271 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
272 // This is a movement of the mouse pointer.
Arpit Singh5cd74972024-11-25 11:17:42 +0000273 processPointerDeviceMotionEventLocked(/*byref*/ newArgs, /*byref*/ pc);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900274 } else {
275 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900276 const auto [x, y] = pc.getPosition();
277 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
278 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
279 args.pointerCoords[i].getX() + x);
280 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
281 args.pointerCoords[i].getY() + y);
282 }
283 newArgs.xCursorPosition = x;
284 newArgs.yCursorPosition = y;
285 }
Arpit Singhe33845c2024-10-25 20:59:24 +0000286
287 // Note displayId may have changed if the cursor moved to a different display
288 if (canUnfadeOnDisplay(newArgs.displayId)) {
Arpit Singh5cd74972024-11-25 11:17:42 +0000289 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
290 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900291 return newArgs;
292}
293
Arpit Singh5cd74972024-11-25 11:17:42 +0000294void PointerChoreographer::processPointerDeviceMotionEventLocked(NotifyMotionArgs& newArgs,
295 PointerControllerInterface& pc) {
296 const float deltaX = newArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
297 const float deltaY = newArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
298
Arpit Singhe33845c2024-10-25 20:59:24 +0000299 FloatPoint unconsumedDelta = pc.move(deltaX, deltaY);
300 if (com::android::input::flags::connected_displays_cursor() &&
301 (std::abs(unconsumedDelta.x) > 0 || std::abs(unconsumedDelta.y) > 0)) {
302 handleUnconsumedDeltaLocked(pc, unconsumedDelta);
303 // pointer may have moved to a different viewport
304 newArgs.displayId = pc.getDisplayId();
305 }
306
Arpit Singh5cd74972024-11-25 11:17:42 +0000307 const auto [x, y] = pc.getPosition();
308 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
309 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
310 newArgs.xCursorPosition = x;
311 newArgs.yCursorPosition = y;
312}
313
Arpit Singhe33845c2024-10-25 20:59:24 +0000314void PointerChoreographer::handleUnconsumedDeltaLocked(PointerControllerInterface& pc,
315 const FloatPoint& unconsumedDelta) {
316 const ui::LogicalDisplayId sourceDisplayId = pc.getDisplayId();
317 const auto& sourceViewport = *findViewportByIdLocked(sourceDisplayId);
318 std::optional<const DisplayViewport*> destination =
319 findDestinationDisplayLocked(sourceViewport, unconsumedDelta);
320 if (!destination) {
321 // no adjacent display
322 return;
323 }
324
325 const DisplayViewport* destinationViewport = *destination;
326
327 if (mMousePointersByDisplay.find(destinationViewport->displayId) !=
328 mMousePointersByDisplay.end()) {
329 LOG(FATAL) << "A cursor already exists on destination display"
330 << destinationViewport->displayId;
331 }
332 mDefaultMouseDisplayId = destinationViewport->displayId;
333 auto pcNode = mMousePointersByDisplay.extract(sourceDisplayId);
334 pcNode.key() = destinationViewport->displayId;
335 mMousePointersByDisplay.insert(std::move(pcNode));
336
337 // This will place cursor at the center of the target display for now
338 // TODO (b/367660694) place the cursor at the appropriate position in destination display
339 pc.setDisplayViewport(*destinationViewport);
340}
341
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000342void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700343 if (args.displayId == ui::LogicalDisplayId::INVALID) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000344 return;
345 }
346
347 if (args.getPointerCount() != 1) {
348 LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: "
349 << args.dump();
350 }
351
352 // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000353 auto [it, controllerAdded] =
354 mDrawingTabletPointersByDevice.try_emplace(args.deviceId,
355 getMouseControllerConstructor(
356 args.displayId));
357 if (controllerAdded) {
358 onControllerAddedOrRemovedLocked();
359 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000360
361 PointerControllerInterface& pc = *it->second;
362
363 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
364 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
365 pc.setPosition(x, y);
366 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
367 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
368 // immediately by a DOWN event.
369 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
370 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
371 } else if (canUnfadeOnDisplay(args.displayId)) {
372 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
373 }
374}
375
Byoungho Jungda10dd32023-10-06 17:03:45 +0900376/**
377 * When screen is touched, fade the mouse pointer on that display. We only call fade for
378 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
379 * mouse device keeps moving and unfades the cursor.
380 * For touch events, we do not need to populate the cursor position.
381 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900382void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800383 if (!args.displayId.isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900384 return;
385 }
386
Byoungho Jungda10dd32023-10-06 17:03:45 +0900387 if (const auto it = mMousePointersByDisplay.find(args.displayId);
388 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
389 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
390 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900391
392 if (!mShowTouchesEnabled) {
393 return;
394 }
395
396 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000397 auto [it, controllerAdded] =
398 mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
399 if (controllerAdded) {
Arpit Singh420d0742024-04-04 11:54:20 +0000400 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000401 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900402
403 PointerControllerInterface& pc = *it->second;
404
405 const PointerCoords* coords = args.pointerCoords.data();
406 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
407 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
408 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
409 BitSet32 idBits;
Linnan Li45b321e2024-07-17 19:33:21 +0000410 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL &&
411 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900412 for (size_t i = 0; i < args.getPointerCount(); i++) {
413 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
414 continue;
415 }
416 uint32_t id = args.pointerProperties[i].id;
417 idToIndex[id] = i;
418 idBits.markBit(id);
419 }
420 }
421 // The PointerController already handles setting spots per-display, so
422 // we do not need to manually manage display changes for touch spots for now.
423 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000424}
425
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900426void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800427 if (!args.displayId.isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900428 return;
429 }
430
431 if (args.getPointerCount() != 1) {
432 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
433 << args.dump();
434 }
435
436 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000437 auto [it, controllerAdded] =
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900438 mStylusPointersByDevice.try_emplace(args.deviceId,
439 getStylusControllerConstructor(args.displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000440 if (controllerAdded) {
441 onControllerAddedOrRemovedLocked();
442 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900443
444 PointerControllerInterface& pc = *it->second;
445
446 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
447 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
448 pc.setPosition(x, y);
449 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000450 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
451 // immediately by a DOWN event.
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900452 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan888993d2024-09-17 20:01:48 +0000453 pc.updatePointerIcon(mShowTouchesEnabled ? PointerIconStyle::TYPE_SPOT_HOVER
454 : PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000455 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900456 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
457 }
458}
459
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000460void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
461 mNextListener.notify(args);
462}
463
464void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
465 mNextListener.notify(args);
466}
467
468void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
469 mNextListener.notify(args);
470}
471
472void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900473 processDeviceReset(args);
474
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000475 mNextListener.notify(args);
476}
477
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900478void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
Arpit Singh7918c822024-11-20 11:28:44 +0000479 std::scoped_lock _l(getLock());
Prabir Pradhan16788792023-11-08 21:07:21 +0000480 mTouchPointersByDevice.erase(args.deviceId);
481 mStylusPointersByDevice.erase(args.deviceId);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000482 mDrawingTabletPointersByDevice.erase(args.deviceId);
Arpit Singh420d0742024-04-04 11:54:20 +0000483 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000484}
485
Arpit Singh420d0742024-04-04 11:54:20 +0000486void PointerChoreographer::onControllerAddedOrRemovedLocked() {
Arpit Singhe33845c2024-10-25 20:59:24 +0000487 if (!com::android::input::flags::hide_pointer_indicators_for_secure_windows() &&
488 !com::android::input::flags::connected_displays_cursor()) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000489 return;
490 }
Arpit Singh420d0742024-04-04 11:54:20 +0000491 bool requireListener = !mTouchPointersByDevice.empty() || !mMousePointersByDisplay.empty() ||
492 !mDrawingTabletPointersByDevice.empty() || !mStylusPointersByDevice.empty();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000493
Arpit Singh7918c822024-11-20 11:28:44 +0000494 // PointerChoreographer uses Listener's lock which is already held by caller
495 base::ScopedLockAssertion assumeLocked(mWindowInfoListener->mLock);
496
497 if (requireListener && !mIsWindowInfoListenerRegistered) {
498 mIsWindowInfoListenerRegistered = true;
499 mWindowInfoListener->setInitialDisplayInfosLocked(mRegisterListener(mWindowInfoListener));
500 onPrivacySensitiveDisplaysChangedLocked(
501 mWindowInfoListener->getPrivacySensitiveDisplaysLocked());
502 } else if (!requireListener && mIsWindowInfoListenerRegistered) {
503 mIsWindowInfoListenerRegistered = false;
Arpit Singhbd49b282024-05-23 18:02:54 +0000504 mUnregisterListener(mWindowInfoListener);
Arpit Singh7918c822024-11-20 11:28:44 +0000505 } else if (requireListener) {
Arpit Singh420d0742024-04-04 11:54:20 +0000506 // controller may have been added to an existing privacy sensitive display, we need to
507 // update all controllers again
Arpit Singh7918c822024-11-20 11:28:44 +0000508 onPrivacySensitiveDisplaysChangedLocked(
509 mWindowInfoListener->getPrivacySensitiveDisplaysLocked());
Arpit Singh420d0742024-04-04 11:54:20 +0000510 }
511}
512
513void PointerChoreographer::onPrivacySensitiveDisplaysChangedLocked(
514 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) {
515 for (auto& [_, pc] : mTouchPointersByDevice) {
516 pc->clearSkipScreenshotFlags();
517 for (auto displayId : privacySensitiveDisplays) {
518 pc->setSkipScreenshotFlagForDisplay(displayId);
519 }
520 }
521
522 for (auto& [displayId, pc] : mMousePointersByDisplay) {
523 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
524 pc->setSkipScreenshotFlagForDisplay(displayId);
525 } else {
526 pc->clearSkipScreenshotFlags();
527 }
528 }
529
530 for (auto* pointerControllerByDevice :
531 {&mDrawingTabletPointersByDevice, &mStylusPointersByDevice}) {
532 for (auto& [_, pc] : *pointerControllerByDevice) {
533 auto displayId = pc->getDisplayId();
534 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
535 pc->setSkipScreenshotFlagForDisplay(displayId);
536 } else {
537 pc->clearSkipScreenshotFlags();
538 }
539 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000540 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900541}
542
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000543void PointerChoreographer::notifyPointerCaptureChanged(
544 const NotifyPointerCaptureChangedArgs& args) {
Hiroki Sato25040232024-02-22 17:21:22 +0900545 if (args.request.isEnable()) {
Arpit Singh7918c822024-11-20 11:28:44 +0000546 std::scoped_lock _l(getLock());
Byoungho Jungda10dd32023-10-06 17:03:45 +0900547 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
548 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
549 }
550 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000551 mNextListener.notify(args);
552}
553
Arpit Singhe33845c2024-10-25 20:59:24 +0000554void PointerChoreographer::setDisplayTopology(
555 const std::unordered_map<ui::LogicalDisplayId, std::vector<AdjacentDisplay>>&
556 displayTopology) {
557 std::scoped_lock _l(getLock());
558 mTopology = displayTopology;
559}
560
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000561void PointerChoreographer::dump(std::string& dump) {
Arpit Singh7918c822024-11-20 11:28:44 +0000562 std::scoped_lock _l(getLock());
Byoungho Jungda10dd32023-10-06 17:03:45 +0900563
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000564 dump += "PointerChoreographer:\n";
Harry Cuttsebd418a2024-08-16 15:52:24 +0000565 dump += StringPrintf(INDENT "Show Touches Enabled: %s\n",
566 mShowTouchesEnabled ? "true" : "false");
567 dump += StringPrintf(INDENT "Stylus PointerIcon Enabled: %s\n",
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900568 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900569
570 dump += INDENT "MousePointerControllers:\n";
571 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
572 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
Linnan Li13bf76a2024-05-05 19:18:02 +0800573 dump += INDENT + displayId.toString() + " : " + pointerControllerDump;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900574 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900575 dump += INDENT "TouchPointerControllers:\n";
576 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
577 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
578 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
579 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900580 dump += INDENT "StylusPointerControllers:\n";
581 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
582 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
583 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
584 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000585 dump += INDENT "DrawingTabletControllers:\n";
586 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
587 std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT);
588 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
589 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900590 dump += "\n";
591}
592
Linnan Li13bf76a2024-05-05 19:18:02 +0800593const DisplayViewport* PointerChoreographer::findViewportByIdLocked(
594 ui::LogicalDisplayId displayId) const {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900595 for (auto& viewport : mViewports) {
596 if (viewport.displayId == displayId) {
597 return &viewport;
598 }
599 }
600 return nullptr;
601}
602
Linnan Li13bf76a2024-05-05 19:18:02 +0800603ui::LogicalDisplayId PointerChoreographer::getTargetMouseDisplayLocked(
604 ui::LogicalDisplayId associatedDisplayId) const {
605 return associatedDisplayId.isValid() ? associatedDisplayId : mDefaultMouseDisplayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900606}
607
Linnan Li13bf76a2024-05-05 19:18:02 +0800608std::pair<ui::LogicalDisplayId, PointerControllerInterface&>
609PointerChoreographer::ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) {
610 const ui::LogicalDisplayId displayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900611
Prabir Pradhan990d8712024-03-05 00:31:36 +0000612 auto it = mMousePointersByDisplay.find(displayId);
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000613 if (it == mMousePointersByDisplay.end()) {
614 it = mMousePointersByDisplay.emplace(displayId, getMouseControllerConstructor(displayId))
615 .first;
Arpit Singh420d0742024-04-04 11:54:20 +0000616 onControllerAddedOrRemovedLocked();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000617 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900618
619 return {displayId, *it->second};
620}
621
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900622InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000623 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
624 [deviceId](const auto& info) { return info.getId() == deviceId; });
625 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900626}
627
Linnan Li13bf76a2024-05-05 19:18:02 +0800628bool PointerChoreographer::canUnfadeOnDisplay(ui::LogicalDisplayId displayId) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000629 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
630}
631
Arpit Singh7918c822024-11-20 11:28:44 +0000632std::mutex& PointerChoreographer::getLock() const {
633 return mWindowInfoListener->mLock;
634}
635
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000636PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() {
Linnan Li13bf76a2024-05-05 19:18:02 +0800637 std::set<ui::LogicalDisplayId /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900638 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900639 std::set<DeviceId> stylusDevicesToKeep;
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000640 std::set<DeviceId> drawingTabletDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900641
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000642 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
643 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900644 for (const auto& info : mInputDeviceInfos) {
Linnan Li48f80da2024-04-22 18:38:16 +0000645 if (!info.isEnabled()) {
646 // If device is disabled, we should not keep it, and should not show pointer for
647 // disabled mouse device.
648 continue;
649 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900650 const uint32_t sources = info.getSources();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000651 const bool isKnownMouse = mMouseDevices.count(info.getId()) != 0;
652
653 if (isMouseOrTouchpad(sources) || isKnownMouse) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800654 const ui::LogicalDisplayId displayId =
655 getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000656 mouseDisplaysToKeep.insert(displayId);
657 // For mice, show the cursor immediately when the device is first connected or
658 // when it moves to a new display.
659 auto [mousePointerIt, isNewMousePointer] =
660 mMousePointersByDisplay.try_emplace(displayId,
661 getMouseControllerConstructor(displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000662 if (isNewMousePointer) {
663 onControllerAddedOrRemovedLocked();
664 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000665
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000666 mMouseDevices.emplace(info.getId());
667 if ((!isKnownMouse || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000668 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
669 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900670 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900671 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800672 info.getAssociatedDisplayId().isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900673 touchDevicesToKeep.insert(info.getId());
674 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900675 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800676 info.getAssociatedDisplayId().isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900677 stylusDevicesToKeep.insert(info.getId());
678 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000679 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800680 info.getAssociatedDisplayId().isValid()) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000681 drawingTabletDevicesToKeep.insert(info.getId());
682 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900683 }
684
685 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000686 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000687 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900688 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900689 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000690 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900691 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900692 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000693 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900694 });
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000695 std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) {
696 return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end();
697 });
Arpit Singh7918c822024-11-20 11:28:44 +0000698 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(getLock()) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000699 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
700 [id](const auto& info) { return info.getId() == id; }) ==
701 mInputDeviceInfos.end();
702 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900703
Arpit Singh420d0742024-04-04 11:54:20 +0000704 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000705
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000706 // Check if we need to notify the policy if there's a change on the pointer display ID.
707 return calculatePointerDisplayChangeToNotify();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900708}
709
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000710PointerChoreographer::PointerDisplayChange
711PointerChoreographer::calculatePointerDisplayChangeToNotify() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700712 ui::LogicalDisplayId displayIdToNotify = ui::LogicalDisplayId::INVALID;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900713 FloatPoint cursorPosition = {0, 0};
714 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
715 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000716 const auto& pointerController = it->second;
717 // Use the displayId from the pointerController, because it accurately reflects whether
718 // the viewport has been added for that display. Otherwise, we would have to check if
719 // the viewport exists separately.
720 displayIdToNotify = pointerController->getDisplayId();
721 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900722 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900723 if (mNotifiedPointerDisplayId == displayIdToNotify) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000724 return {};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900725 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900726 mNotifiedPointerDisplayId = displayIdToNotify;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000727 return {{displayIdToNotify, cursorPosition}};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900728}
729
Linnan Li13bf76a2024-05-05 19:18:02 +0800730void PointerChoreographer::setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000731 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900732
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000733 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000734 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000735
736 mDefaultMouseDisplayId = displayId;
737 pointerDisplayChange = updatePointerControllersLocked();
738 } // release lock
739
740 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900741}
742
743void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000744 PointerDisplayChange pointerDisplayChange;
745
746 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000747 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000748 for (const auto& viewport : viewports) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800749 const ui::LogicalDisplayId displayId = viewport.displayId;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000750 if (const auto it = mMousePointersByDisplay.find(displayId);
751 it != mMousePointersByDisplay.end()) {
752 it->second->setDisplayViewport(viewport);
753 }
754 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
755 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
756 if (info && info->getAssociatedDisplayId() == displayId) {
757 stylusPointerController->setDisplayViewport(viewport);
758 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900759 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000760 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
761 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
762 if (info && info->getAssociatedDisplayId() == displayId) {
763 drawingTabletController->setDisplayViewport(viewport);
764 }
765 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900766 }
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000767 mViewports = viewports;
768 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
769 } // release lock
770
771 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900772}
773
774std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
Linnan Li13bf76a2024-05-05 19:18:02 +0800775 ui::LogicalDisplayId associatedDisplayId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000776 std::scoped_lock _l(getLock());
Linnan Li13bf76a2024-05-05 19:18:02 +0800777 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900778 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
779 return *viewport;
780 }
781 return std::nullopt;
782}
783
Linnan Li13bf76a2024-05-05 19:18:02 +0800784FloatPoint PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000785 std::scoped_lock _l(getLock());
Linnan Li13bf76a2024-05-05 19:18:02 +0800786 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900787 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
788 it != mMousePointersByDisplay.end()) {
789 return it->second->getPosition();
790 }
791 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000792}
793
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900794void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000795 PointerDisplayChange pointerDisplayChange;
796
797 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000798 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000799 if (mShowTouchesEnabled == enabled) {
800 return;
801 }
802 mShowTouchesEnabled = enabled;
803 pointerDisplayChange = updatePointerControllersLocked();
804 } // release lock
805
806 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900807}
808
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900809void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000810 PointerDisplayChange pointerDisplayChange;
811
812 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000813 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000814 if (mStylusPointerIconEnabled == enabled) {
815 return;
816 }
817 mStylusPointerIconEnabled = enabled;
818 pointerDisplayChange = updatePointerControllersLocked();
819 } // release lock
820
821 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900822}
823
Byoungho Jung99326452023-11-03 20:19:17 +0900824bool PointerChoreographer::setPointerIcon(
Linnan Li13bf76a2024-05-05 19:18:02 +0800825 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
826 ui::LogicalDisplayId displayId, DeviceId deviceId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000827 std::scoped_lock _l(getLock());
Byoungho Jung99326452023-11-03 20:19:17 +0900828 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000829 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900830 return false;
831 }
832 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
833 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000834 LOG(WARNING) << "No input device info found for id " << deviceId
835 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900836 return false;
837 }
838 const uint32_t sources = info->getSources();
Byoungho Jung99326452023-11-03 20:19:17 +0900839
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000840 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) {
841 auto it = mDrawingTabletPointersByDevice.find(deviceId);
842 if (it != mDrawingTabletPointersByDevice.end()) {
843 setIconForController(icon, *it->second);
844 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900845 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000846 }
847 if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) {
848 auto it = mStylusPointersByDevice.find(deviceId);
849 if (it != mStylusPointersByDevice.end()) {
Prabir Pradhan888993d2024-09-17 20:01:48 +0000850 if (mShowTouchesEnabled) {
851 // If an app doesn't override the icon for the hovering stylus, show the hover icon.
852 auto* style = std::get_if<PointerIconStyle>(&icon);
853 if (style != nullptr && *style == PointerIconStyle::TYPE_NOT_SPECIFIED) {
854 *style = PointerIconStyle::TYPE_SPOT_HOVER;
855 }
856 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000857 setIconForController(icon, *it->second);
858 return true;
859 }
860 }
861 if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
862 auto it = mMousePointersByDisplay.find(displayId);
863 if (it != mMousePointersByDisplay.end()) {
864 setIconForController(icon, *it->second);
865 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900866 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000867 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
868 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900869 return false;
870 }
Byoungho Jung99326452023-11-03 20:19:17 +0900871 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000872 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId
873 << ".";
874 return false;
Byoungho Jung99326452023-11-03 20:19:17 +0900875}
876
Linnan Li13bf76a2024-05-05 19:18:02 +0800877void PointerChoreographer::setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) {
Arpit Singh7918c822024-11-20 11:28:44 +0000878 std::scoped_lock lock(getLock());
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000879 if (visible) {
880 mDisplaysWithPointersHidden.erase(displayId);
881 // We do not unfade the icons here, because we don't know when the last event happened.
882 return;
883 }
884
885 mDisplaysWithPointersHidden.emplace(displayId);
886
887 // Hide any icons that are currently visible on the display.
888 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
889 const auto& [_, controller] = *it;
890 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
891 }
892 for (const auto& [_, controller] : mStylusPointersByDevice) {
893 if (controller->getDisplayId() == displayId) {
894 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
895 }
896 }
897}
898
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000899void PointerChoreographer::setFocusedDisplay(ui::LogicalDisplayId displayId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000900 std::scoped_lock lock(getLock());
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000901 mCurrentFocusedDisplay = displayId;
902}
903
Prabir Pradhan19767602023-11-03 16:53:31 +0000904PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800905 ui::LogicalDisplayId displayId) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000906 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
Arpit Singh7918c822024-11-20 11:28:44 +0000907 [this, displayId]() REQUIRES(getLock()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000908 auto pc = mPolicy.createPointerController(
909 PointerControllerInterface::ControllerType::MOUSE);
910 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
911 pc->setDisplayViewport(*viewport);
912 }
913 return pc;
914 };
915 return ConstructorDelegate(std::move(ctor));
916}
917
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900918PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800919 ui::LogicalDisplayId displayId) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900920 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
Arpit Singh7918c822024-11-20 11:28:44 +0000921 [this, displayId]() REQUIRES(getLock()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900922 auto pc = mPolicy.createPointerController(
923 PointerControllerInterface::ControllerType::STYLUS);
924 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
925 pc->setDisplayViewport(*viewport);
926 }
927 return pc;
928 };
929 return ConstructorDelegate(std::move(ctor));
930}
931
Arpit Singhe33845c2024-10-25 20:59:24 +0000932void PointerChoreographer::populateFakeDisplayTopologyLocked(
933 const std::vector<gui::DisplayInfo>& displayInfos) {
934 if (!com::android::input::flags::connected_displays_cursor()) {
935 return;
936 }
937
938 if (displayInfos.size() == mTopology.size()) {
939 bool displaysChanged = false;
940 for (const auto& displayInfo : displayInfos) {
941 if (mTopology.find(displayInfo.displayId) == mTopology.end()) {
942 displaysChanged = true;
943 break;
944 }
945 }
946
947 if (!displaysChanged) {
948 return;
949 }
950 }
951
952 // create a fake topology assuming following order
953 // default-display (top-edge) -> next-display (right-edge) -> next-display (right-edge) ...
954 // ┌─────────┬─────────┐
955 // │ next │ next 2 │ ...
956 // ├─────────┼─────────┘
957 // │ default │
958 // └─────────┘
959 mTopology.clear();
960
961 // treat default display as base, in real topology it should be the primary-display
962 ui::LogicalDisplayId previousDisplay = ui::LogicalDisplayId::DEFAULT;
963 for (const auto& displayInfo : displayInfos) {
964 if (displayInfo.displayId == ui::LogicalDisplayId::DEFAULT) {
965 continue;
966 }
967 if (previousDisplay == ui::LogicalDisplayId::DEFAULT) {
968 mTopology[previousDisplay].push_back({displayInfo.displayId, DisplayPosition::TOP, 0});
969 mTopology[displayInfo.displayId].push_back(
970 {previousDisplay, DisplayPosition::BOTTOM, 0});
971 } else {
972 mTopology[previousDisplay].push_back(
973 {displayInfo.displayId, DisplayPosition::RIGHT, 0});
974 mTopology[displayInfo.displayId].push_back({previousDisplay, DisplayPosition::LEFT, 0});
975 }
976 previousDisplay = displayInfo.displayId;
977 }
978
979 // update default pointer display. In real topology it should be the primary-display
980 if (mTopology.find(mDefaultMouseDisplayId) == mTopology.end()) {
981 mDefaultMouseDisplayId = ui::LogicalDisplayId::DEFAULT;
982 }
983}
984
985std::optional<const DisplayViewport*> PointerChoreographer::findDestinationDisplayLocked(
986 const DisplayViewport& sourceViewport, const FloatPoint& unconsumedDelta) const {
987 DisplayPosition sourceBoundary;
988 if (unconsumedDelta.x > 0) {
989 sourceBoundary = DisplayPosition::RIGHT;
990 } else if (unconsumedDelta.x < 0) {
991 sourceBoundary = DisplayPosition::LEFT;
992 } else if (unconsumedDelta.y > 0) {
993 sourceBoundary = DisplayPosition::BOTTOM;
994 } else {
995 sourceBoundary = DisplayPosition::TOP;
996 }
997
998 // Choreographer works in un-rotate coordinate space so we need to rotate boundary by viewport
999 // orientation to find the rotated boundary
1000 constexpr int MOD = ftl::to_underlying(ui::Rotation::ftl_last) + 1;
1001 sourceBoundary = static_cast<DisplayPosition>(
1002 (ftl::to_underlying(sourceBoundary) + ftl::to_underlying(sourceViewport.orientation)) %
1003 MOD);
1004
1005 const auto& destination = mTopology.find(sourceViewport.displayId);
1006 if (destination == mTopology.end()) {
1007 // Topology is likely out of sync with viewport info, wait for it to be updated
1008 LOG(WARNING) << "Source display missing from topology " << sourceViewport.displayId;
1009 return std::nullopt;
1010 }
1011
1012 for (const auto& adjacentDisplay : destination->second) {
1013 if (adjacentDisplay.position != sourceBoundary) {
1014 continue;
1015 }
1016 const DisplayViewport* destinationViewport =
1017 findViewportByIdLocked(adjacentDisplay.displayId);
1018 if (destinationViewport == nullptr) {
1019 // Topology is likely out of sync with viewport info, wait for them to be updated
1020 LOG(WARNING) << "Cannot find viewport for adjacent display "
1021 << adjacentDisplay.displayId << "of source display "
1022 << sourceViewport.displayId;
1023 break;
1024 }
1025 return destinationViewport;
1026 }
1027 return std::nullopt;
1028}
1029
Arpit Singh7918c822024-11-20 11:28:44 +00001030// --- PointerChoreographer::PointerChoreographerDisplayInfoListener ---
1031
Arpit Singh4b6ad2d2024-04-04 11:54:20 +00001032void PointerChoreographer::PointerChoreographerDisplayInfoListener::onWindowInfosChanged(
1033 const gui::WindowInfosUpdate& windowInfosUpdate) {
Arpit Singh7918c822024-11-20 11:28:44 +00001034 std::scoped_lock _l(mLock);
Arpit Singh420d0742024-04-04 11:54:20 +00001035 if (mPointerChoreographer == nullptr) {
1036 return;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +00001037 }
Arpit Singh420d0742024-04-04 11:54:20 +00001038 auto newPrivacySensitiveDisplays =
1039 getPrivacySensitiveDisplaysFromWindowInfos(windowInfosUpdate.windowInfos);
Arpit Singhe33845c2024-10-25 20:59:24 +00001040
1041 // PointerChoreographer uses Listener's lock.
1042 base::ScopedLockAssertion assumeLocked(mPointerChoreographer->getLock());
Arpit Singh420d0742024-04-04 11:54:20 +00001043 if (newPrivacySensitiveDisplays != mPrivacySensitiveDisplays) {
1044 mPrivacySensitiveDisplays = std::move(newPrivacySensitiveDisplays);
Arpit Singh7918c822024-11-20 11:28:44 +00001045 mPointerChoreographer->onPrivacySensitiveDisplaysChangedLocked(mPrivacySensitiveDisplays);
Arpit Singh420d0742024-04-04 11:54:20 +00001046 }
Arpit Singhe33845c2024-10-25 20:59:24 +00001047 mPointerChoreographer->populateFakeDisplayTopologyLocked(windowInfosUpdate.displayInfos);
Arpit Singh420d0742024-04-04 11:54:20 +00001048}
1049
Arpit Singh7918c822024-11-20 11:28:44 +00001050void PointerChoreographer::PointerChoreographerDisplayInfoListener::setInitialDisplayInfosLocked(
Arpit Singh420d0742024-04-04 11:54:20 +00001051 const std::vector<gui::WindowInfo>& windowInfos) {
Arpit Singh420d0742024-04-04 11:54:20 +00001052 mPrivacySensitiveDisplays = getPrivacySensitiveDisplaysFromWindowInfos(windowInfos);
1053}
1054
1055std::unordered_set<ui::LogicalDisplayId /*displayId*/>
Arpit Singh7918c822024-11-20 11:28:44 +00001056PointerChoreographer::PointerChoreographerDisplayInfoListener::getPrivacySensitiveDisplaysLocked() {
Arpit Singh420d0742024-04-04 11:54:20 +00001057 return mPrivacySensitiveDisplays;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +00001058}
1059
1060void PointerChoreographer::PointerChoreographerDisplayInfoListener::
1061 onPointerChoreographerDestroyed() {
Arpit Singh7918c822024-11-20 11:28:44 +00001062 std::scoped_lock _l(mLock);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +00001063 mPointerChoreographer = nullptr;
1064}
1065
Prabir Pradhanb56e92c2023-06-09 23:40:37 +00001066} // namespace android