blob: 38e5974c3f1fa135eeacfacfc2504f6a16200936 [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 Singh7918c822024-11-20 11:28:44 +0000208 std::scoped_lock _l(getLock());
Byoungho Jungda10dd32023-10-06 17:03:45 +0900209
Liana Kazanova (xWF)ff80e302024-11-18 19:14:42 +0000210 if (isFromMouse(args)) {
211 return processMouseEventLocked(args);
212 } else if (isFromTouchpad(args)) {
213 return processTouchpadEventLocked(args);
214 } else if (isFromDrawingTablet(args)) {
215 processDrawingTabletEventLocked(args);
216 } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) {
217 processStylusHoverEventLocked(args);
218 } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) {
219 processTouchscreenAndStylusEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900220 }
Liana Kazanova (xWF)ff80e302024-11-18 19:14:42 +0000221 return args;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900222}
223
224NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) {
225 if (args.getPointerCount() != 1) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000226 LOG(FATAL) << "Only mouse events with a single pointer are currently supported: "
227 << args.dump();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900228 }
229
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000230 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000231 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900232 NotifyMotionArgs newArgs(args);
233 newArgs.displayId = displayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900234
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900235 if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) {
236 // This is an absolute mouse device that knows about the location of the cursor on the
237 // display, so set the cursor position to the specified location.
238 const auto [x, y] = pc.getPosition();
239 const float deltaX = args.xCursorPosition - x;
240 const float deltaY = args.yCursorPosition - y;
241 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
242 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
243 pc.setPosition(args.xCursorPosition, args.yCursorPosition);
244 } else {
245 // This is a relative mouse, so move the cursor by the specified amount.
Arpit Singh5cd74972024-11-25 11:17:42 +0000246 processPointerDeviceMotionEventLocked(/*byref*/ newArgs, /*byref*/ pc);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900247 }
Liana Kazanova (xWF)ff80e302024-11-18 19:14:42 +0000248 if (canUnfadeOnDisplay(displayId)) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000249 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
250 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900251 return newArgs;
252}
253
Byoungho Jungee6268f2023-10-30 17:27:26 +0900254NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000255 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000256 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900257
258 NotifyMotionArgs newArgs(args);
259 newArgs.displayId = displayId;
260 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
261 // This is a movement of the mouse pointer.
Arpit Singh5cd74972024-11-25 11:17:42 +0000262 processPointerDeviceMotionEventLocked(/*byref*/ newArgs, /*byref*/ pc);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900263 } else {
264 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
Byoungho Jungee6268f2023-10-30 17:27:26 +0900265 const auto [x, y] = pc.getPosition();
266 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
267 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
268 args.pointerCoords[i].getX() + x);
269 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
270 args.pointerCoords[i].getY() + y);
271 }
272 newArgs.xCursorPosition = x;
273 newArgs.yCursorPosition = y;
274 }
Arpit Singh5cd74972024-11-25 11:17:42 +0000275 if (canUnfadeOnDisplay(displayId)) {
276 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
277 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900278 return newArgs;
279}
280
Arpit Singh5cd74972024-11-25 11:17:42 +0000281void PointerChoreographer::processPointerDeviceMotionEventLocked(NotifyMotionArgs& newArgs,
282 PointerControllerInterface& pc) {
283 const float deltaX = newArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
284 const float deltaY = newArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
285
286 pc.move(deltaX, deltaY);
287 const auto [x, y] = pc.getPosition();
288 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
289 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
290 newArgs.xCursorPosition = x;
291 newArgs.yCursorPosition = y;
292}
293
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000294void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700295 if (args.displayId == ui::LogicalDisplayId::INVALID) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000296 return;
297 }
298
299 if (args.getPointerCount() != 1) {
300 LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: "
301 << args.dump();
302 }
303
304 // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000305 auto [it, controllerAdded] =
306 mDrawingTabletPointersByDevice.try_emplace(args.deviceId,
307 getMouseControllerConstructor(
308 args.displayId));
309 if (controllerAdded) {
310 onControllerAddedOrRemovedLocked();
311 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000312
313 PointerControllerInterface& pc = *it->second;
314
315 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
316 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
317 pc.setPosition(x, y);
318 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
319 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
320 // immediately by a DOWN event.
321 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
322 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
323 } else if (canUnfadeOnDisplay(args.displayId)) {
324 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
325 }
326}
327
Byoungho Jungda10dd32023-10-06 17:03:45 +0900328/**
329 * When screen is touched, fade the mouse pointer on that display. We only call fade for
330 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
331 * mouse device keeps moving and unfades the cursor.
332 * For touch events, we do not need to populate the cursor position.
333 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900334void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800335 if (!args.displayId.isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900336 return;
337 }
338
Byoungho Jungda10dd32023-10-06 17:03:45 +0900339 if (const auto it = mMousePointersByDisplay.find(args.displayId);
340 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
341 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
342 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900343
344 if (!mShowTouchesEnabled) {
345 return;
346 }
347
348 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000349 auto [it, controllerAdded] =
350 mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
351 if (controllerAdded) {
Arpit Singh420d0742024-04-04 11:54:20 +0000352 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000353 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900354
355 PointerControllerInterface& pc = *it->second;
356
357 const PointerCoords* coords = args.pointerCoords.data();
358 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
359 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
360 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
361 BitSet32 idBits;
Linnan Li45b321e2024-07-17 19:33:21 +0000362 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL &&
363 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900364 for (size_t i = 0; i < args.getPointerCount(); i++) {
365 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
366 continue;
367 }
368 uint32_t id = args.pointerProperties[i].id;
369 idToIndex[id] = i;
370 idBits.markBit(id);
371 }
372 }
373 // The PointerController already handles setting spots per-display, so
374 // we do not need to manually manage display changes for touch spots for now.
375 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000376}
377
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900378void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800379 if (!args.displayId.isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900380 return;
381 }
382
383 if (args.getPointerCount() != 1) {
384 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
385 << args.dump();
386 }
387
388 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000389 auto [it, controllerAdded] =
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900390 mStylusPointersByDevice.try_emplace(args.deviceId,
391 getStylusControllerConstructor(args.displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000392 if (controllerAdded) {
393 onControllerAddedOrRemovedLocked();
394 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900395
396 PointerControllerInterface& pc = *it->second;
397
398 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
399 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
400 pc.setPosition(x, y);
401 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000402 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
403 // immediately by a DOWN event.
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900404 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan888993d2024-09-17 20:01:48 +0000405 pc.updatePointerIcon(mShowTouchesEnabled ? PointerIconStyle::TYPE_SPOT_HOVER
406 : PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000407 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900408 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
409 }
410}
411
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000412void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
413 mNextListener.notify(args);
414}
415
416void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
417 mNextListener.notify(args);
418}
419
420void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
421 mNextListener.notify(args);
422}
423
424void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900425 processDeviceReset(args);
426
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000427 mNextListener.notify(args);
428}
429
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900430void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
Arpit Singh7918c822024-11-20 11:28:44 +0000431 std::scoped_lock _l(getLock());
Prabir Pradhan16788792023-11-08 21:07:21 +0000432 mTouchPointersByDevice.erase(args.deviceId);
433 mStylusPointersByDevice.erase(args.deviceId);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000434 mDrawingTabletPointersByDevice.erase(args.deviceId);
Arpit Singh420d0742024-04-04 11:54:20 +0000435 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000436}
437
Arpit Singh420d0742024-04-04 11:54:20 +0000438void PointerChoreographer::onControllerAddedOrRemovedLocked() {
Liana Kazanova (xWF)ff80e302024-11-18 19:14:42 +0000439 if (!com::android::input::flags::hide_pointer_indicators_for_secure_windows()) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000440 return;
441 }
Arpit Singh420d0742024-04-04 11:54:20 +0000442 bool requireListener = !mTouchPointersByDevice.empty() || !mMousePointersByDisplay.empty() ||
443 !mDrawingTabletPointersByDevice.empty() || !mStylusPointersByDevice.empty();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000444
Arpit Singh7918c822024-11-20 11:28:44 +0000445 // PointerChoreographer uses Listener's lock which is already held by caller
446 base::ScopedLockAssertion assumeLocked(mWindowInfoListener->mLock);
447
448 if (requireListener && !mIsWindowInfoListenerRegistered) {
449 mIsWindowInfoListenerRegistered = true;
450 mWindowInfoListener->setInitialDisplayInfosLocked(mRegisterListener(mWindowInfoListener));
451 onPrivacySensitiveDisplaysChangedLocked(
452 mWindowInfoListener->getPrivacySensitiveDisplaysLocked());
453 } else if (!requireListener && mIsWindowInfoListenerRegistered) {
454 mIsWindowInfoListenerRegistered = false;
Arpit Singhbd49b282024-05-23 18:02:54 +0000455 mUnregisterListener(mWindowInfoListener);
Arpit Singh7918c822024-11-20 11:28:44 +0000456 } else if (requireListener) {
Arpit Singh420d0742024-04-04 11:54:20 +0000457 // controller may have been added to an existing privacy sensitive display, we need to
458 // update all controllers again
Arpit Singh7918c822024-11-20 11:28:44 +0000459 onPrivacySensitiveDisplaysChangedLocked(
460 mWindowInfoListener->getPrivacySensitiveDisplaysLocked());
Arpit Singh420d0742024-04-04 11:54:20 +0000461 }
462}
463
464void PointerChoreographer::onPrivacySensitiveDisplaysChangedLocked(
465 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) {
466 for (auto& [_, pc] : mTouchPointersByDevice) {
467 pc->clearSkipScreenshotFlags();
468 for (auto displayId : privacySensitiveDisplays) {
469 pc->setSkipScreenshotFlagForDisplay(displayId);
470 }
471 }
472
473 for (auto& [displayId, pc] : mMousePointersByDisplay) {
474 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
475 pc->setSkipScreenshotFlagForDisplay(displayId);
476 } else {
477 pc->clearSkipScreenshotFlags();
478 }
479 }
480
481 for (auto* pointerControllerByDevice :
482 {&mDrawingTabletPointersByDevice, &mStylusPointersByDevice}) {
483 for (auto& [_, pc] : *pointerControllerByDevice) {
484 auto displayId = pc->getDisplayId();
485 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
486 pc->setSkipScreenshotFlagForDisplay(displayId);
487 } else {
488 pc->clearSkipScreenshotFlags();
489 }
490 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000491 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900492}
493
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000494void PointerChoreographer::notifyPointerCaptureChanged(
495 const NotifyPointerCaptureChangedArgs& args) {
Hiroki Sato25040232024-02-22 17:21:22 +0900496 if (args.request.isEnable()) {
Arpit Singh7918c822024-11-20 11:28:44 +0000497 std::scoped_lock _l(getLock());
Byoungho Jungda10dd32023-10-06 17:03:45 +0900498 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
499 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
500 }
501 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000502 mNextListener.notify(args);
503}
504
505void PointerChoreographer::dump(std::string& dump) {
Arpit Singh7918c822024-11-20 11:28:44 +0000506 std::scoped_lock _l(getLock());
Byoungho Jungda10dd32023-10-06 17:03:45 +0900507
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000508 dump += "PointerChoreographer:\n";
Harry Cuttsebd418a2024-08-16 15:52:24 +0000509 dump += StringPrintf(INDENT "Show Touches Enabled: %s\n",
510 mShowTouchesEnabled ? "true" : "false");
511 dump += StringPrintf(INDENT "Stylus PointerIcon Enabled: %s\n",
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900512 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900513
514 dump += INDENT "MousePointerControllers:\n";
515 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
516 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
Linnan Li13bf76a2024-05-05 19:18:02 +0800517 dump += INDENT + displayId.toString() + " : " + pointerControllerDump;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900518 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900519 dump += INDENT "TouchPointerControllers:\n";
520 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
521 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
522 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
523 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900524 dump += INDENT "StylusPointerControllers:\n";
525 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
526 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
527 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
528 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000529 dump += INDENT "DrawingTabletControllers:\n";
530 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
531 std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT);
532 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
533 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900534 dump += "\n";
535}
536
Linnan Li13bf76a2024-05-05 19:18:02 +0800537const DisplayViewport* PointerChoreographer::findViewportByIdLocked(
538 ui::LogicalDisplayId displayId) const {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900539 for (auto& viewport : mViewports) {
540 if (viewport.displayId == displayId) {
541 return &viewport;
542 }
543 }
544 return nullptr;
545}
546
Linnan Li13bf76a2024-05-05 19:18:02 +0800547ui::LogicalDisplayId PointerChoreographer::getTargetMouseDisplayLocked(
548 ui::LogicalDisplayId associatedDisplayId) const {
549 return associatedDisplayId.isValid() ? associatedDisplayId : mDefaultMouseDisplayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900550}
551
Linnan Li13bf76a2024-05-05 19:18:02 +0800552std::pair<ui::LogicalDisplayId, PointerControllerInterface&>
553PointerChoreographer::ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) {
554 const ui::LogicalDisplayId displayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900555
Prabir Pradhan990d8712024-03-05 00:31:36 +0000556 auto it = mMousePointersByDisplay.find(displayId);
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000557 if (it == mMousePointersByDisplay.end()) {
558 it = mMousePointersByDisplay.emplace(displayId, getMouseControllerConstructor(displayId))
559 .first;
Arpit Singh420d0742024-04-04 11:54:20 +0000560 onControllerAddedOrRemovedLocked();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000561 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900562
563 return {displayId, *it->second};
564}
565
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900566InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000567 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
568 [deviceId](const auto& info) { return info.getId() == deviceId; });
569 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900570}
571
Linnan Li13bf76a2024-05-05 19:18:02 +0800572bool PointerChoreographer::canUnfadeOnDisplay(ui::LogicalDisplayId displayId) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000573 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
574}
575
Arpit Singh7918c822024-11-20 11:28:44 +0000576std::mutex& PointerChoreographer::getLock() const {
577 return mWindowInfoListener->mLock;
578}
579
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000580PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() {
Linnan Li13bf76a2024-05-05 19:18:02 +0800581 std::set<ui::LogicalDisplayId /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900582 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900583 std::set<DeviceId> stylusDevicesToKeep;
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000584 std::set<DeviceId> drawingTabletDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900585
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000586 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
587 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900588 for (const auto& info : mInputDeviceInfos) {
Linnan Li48f80da2024-04-22 18:38:16 +0000589 if (!info.isEnabled()) {
590 // If device is disabled, we should not keep it, and should not show pointer for
591 // disabled mouse device.
592 continue;
593 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900594 const uint32_t sources = info.getSources();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000595 const bool isKnownMouse = mMouseDevices.count(info.getId()) != 0;
596
597 if (isMouseOrTouchpad(sources) || isKnownMouse) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800598 const ui::LogicalDisplayId displayId =
599 getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000600 mouseDisplaysToKeep.insert(displayId);
601 // For mice, show the cursor immediately when the device is first connected or
602 // when it moves to a new display.
603 auto [mousePointerIt, isNewMousePointer] =
604 mMousePointersByDisplay.try_emplace(displayId,
605 getMouseControllerConstructor(displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000606 if (isNewMousePointer) {
607 onControllerAddedOrRemovedLocked();
608 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000609
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000610 mMouseDevices.emplace(info.getId());
611 if ((!isKnownMouse || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000612 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
613 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900614 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900615 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800616 info.getAssociatedDisplayId().isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900617 touchDevicesToKeep.insert(info.getId());
618 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900619 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800620 info.getAssociatedDisplayId().isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900621 stylusDevicesToKeep.insert(info.getId());
622 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000623 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800624 info.getAssociatedDisplayId().isValid()) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000625 drawingTabletDevicesToKeep.insert(info.getId());
626 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900627 }
628
629 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000630 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000631 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900632 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900633 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000634 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900635 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900636 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000637 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900638 });
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000639 std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) {
640 return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end();
641 });
Arpit Singh7918c822024-11-20 11:28:44 +0000642 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(getLock()) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000643 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
644 [id](const auto& info) { return info.getId() == id; }) ==
645 mInputDeviceInfos.end();
646 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900647
Arpit Singh420d0742024-04-04 11:54:20 +0000648 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000649
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000650 // Check if we need to notify the policy if there's a change on the pointer display ID.
651 return calculatePointerDisplayChangeToNotify();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900652}
653
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000654PointerChoreographer::PointerDisplayChange
655PointerChoreographer::calculatePointerDisplayChangeToNotify() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700656 ui::LogicalDisplayId displayIdToNotify = ui::LogicalDisplayId::INVALID;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900657 FloatPoint cursorPosition = {0, 0};
658 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
659 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000660 const auto& pointerController = it->second;
661 // Use the displayId from the pointerController, because it accurately reflects whether
662 // the viewport has been added for that display. Otherwise, we would have to check if
663 // the viewport exists separately.
664 displayIdToNotify = pointerController->getDisplayId();
665 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900666 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900667 if (mNotifiedPointerDisplayId == displayIdToNotify) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000668 return {};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900669 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900670 mNotifiedPointerDisplayId = displayIdToNotify;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000671 return {{displayIdToNotify, cursorPosition}};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900672}
673
Linnan Li13bf76a2024-05-05 19:18:02 +0800674void PointerChoreographer::setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000675 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900676
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000677 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000678 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000679
680 mDefaultMouseDisplayId = displayId;
681 pointerDisplayChange = updatePointerControllersLocked();
682 } // release lock
683
684 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900685}
686
687void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000688 PointerDisplayChange pointerDisplayChange;
689
690 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000691 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000692 for (const auto& viewport : viewports) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800693 const ui::LogicalDisplayId displayId = viewport.displayId;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000694 if (const auto it = mMousePointersByDisplay.find(displayId);
695 it != mMousePointersByDisplay.end()) {
696 it->second->setDisplayViewport(viewport);
697 }
698 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
699 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
700 if (info && info->getAssociatedDisplayId() == displayId) {
701 stylusPointerController->setDisplayViewport(viewport);
702 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900703 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000704 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
705 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
706 if (info && info->getAssociatedDisplayId() == displayId) {
707 drawingTabletController->setDisplayViewport(viewport);
708 }
709 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900710 }
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000711 mViewports = viewports;
712 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
713 } // release lock
714
715 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900716}
717
718std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
Linnan Li13bf76a2024-05-05 19:18:02 +0800719 ui::LogicalDisplayId associatedDisplayId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000720 std::scoped_lock _l(getLock());
Linnan Li13bf76a2024-05-05 19:18:02 +0800721 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900722 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
723 return *viewport;
724 }
725 return std::nullopt;
726}
727
Linnan Li13bf76a2024-05-05 19:18:02 +0800728FloatPoint PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000729 std::scoped_lock _l(getLock());
Linnan Li13bf76a2024-05-05 19:18:02 +0800730 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900731 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
732 it != mMousePointersByDisplay.end()) {
733 return it->second->getPosition();
734 }
735 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000736}
737
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900738void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000739 PointerDisplayChange pointerDisplayChange;
740
741 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000742 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000743 if (mShowTouchesEnabled == enabled) {
744 return;
745 }
746 mShowTouchesEnabled = enabled;
747 pointerDisplayChange = updatePointerControllersLocked();
748 } // release lock
749
750 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900751}
752
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900753void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000754 PointerDisplayChange pointerDisplayChange;
755
756 { // acquire lock
Arpit Singh7918c822024-11-20 11:28:44 +0000757 std::scoped_lock _l(getLock());
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000758 if (mStylusPointerIconEnabled == enabled) {
759 return;
760 }
761 mStylusPointerIconEnabled = enabled;
762 pointerDisplayChange = updatePointerControllersLocked();
763 } // release lock
764
765 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900766}
767
Byoungho Jung99326452023-11-03 20:19:17 +0900768bool PointerChoreographer::setPointerIcon(
Linnan Li13bf76a2024-05-05 19:18:02 +0800769 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
770 ui::LogicalDisplayId displayId, DeviceId deviceId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000771 std::scoped_lock _l(getLock());
Byoungho Jung99326452023-11-03 20:19:17 +0900772 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000773 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900774 return false;
775 }
776 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
777 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000778 LOG(WARNING) << "No input device info found for id " << deviceId
779 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900780 return false;
781 }
782 const uint32_t sources = info->getSources();
Byoungho Jung99326452023-11-03 20:19:17 +0900783
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000784 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) {
785 auto it = mDrawingTabletPointersByDevice.find(deviceId);
786 if (it != mDrawingTabletPointersByDevice.end()) {
787 setIconForController(icon, *it->second);
788 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900789 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000790 }
791 if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) {
792 auto it = mStylusPointersByDevice.find(deviceId);
793 if (it != mStylusPointersByDevice.end()) {
Prabir Pradhan888993d2024-09-17 20:01:48 +0000794 if (mShowTouchesEnabled) {
795 // If an app doesn't override the icon for the hovering stylus, show the hover icon.
796 auto* style = std::get_if<PointerIconStyle>(&icon);
797 if (style != nullptr && *style == PointerIconStyle::TYPE_NOT_SPECIFIED) {
798 *style = PointerIconStyle::TYPE_SPOT_HOVER;
799 }
800 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000801 setIconForController(icon, *it->second);
802 return true;
803 }
804 }
805 if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
806 auto it = mMousePointersByDisplay.find(displayId);
807 if (it != mMousePointersByDisplay.end()) {
808 setIconForController(icon, *it->second);
809 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900810 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000811 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
812 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900813 return false;
814 }
Byoungho Jung99326452023-11-03 20:19:17 +0900815 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000816 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId
817 << ".";
818 return false;
Byoungho Jung99326452023-11-03 20:19:17 +0900819}
820
Linnan Li13bf76a2024-05-05 19:18:02 +0800821void PointerChoreographer::setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) {
Arpit Singh7918c822024-11-20 11:28:44 +0000822 std::scoped_lock lock(getLock());
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000823 if (visible) {
824 mDisplaysWithPointersHidden.erase(displayId);
825 // We do not unfade the icons here, because we don't know when the last event happened.
826 return;
827 }
828
829 mDisplaysWithPointersHidden.emplace(displayId);
830
831 // Hide any icons that are currently visible on the display.
832 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
833 const auto& [_, controller] = *it;
834 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
835 }
836 for (const auto& [_, controller] : mStylusPointersByDevice) {
837 if (controller->getDisplayId() == displayId) {
838 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
839 }
840 }
841}
842
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000843void PointerChoreographer::setFocusedDisplay(ui::LogicalDisplayId displayId) {
Arpit Singh7918c822024-11-20 11:28:44 +0000844 std::scoped_lock lock(getLock());
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000845 mCurrentFocusedDisplay = displayId;
846}
847
Prabir Pradhan19767602023-11-03 16:53:31 +0000848PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800849 ui::LogicalDisplayId displayId) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000850 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
Arpit Singh7918c822024-11-20 11:28:44 +0000851 [this, displayId]() REQUIRES(getLock()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000852 auto pc = mPolicy.createPointerController(
853 PointerControllerInterface::ControllerType::MOUSE);
854 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
855 pc->setDisplayViewport(*viewport);
856 }
857 return pc;
858 };
859 return ConstructorDelegate(std::move(ctor));
860}
861
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900862PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800863 ui::LogicalDisplayId displayId) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900864 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
Arpit Singh7918c822024-11-20 11:28:44 +0000865 [this, displayId]() REQUIRES(getLock()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900866 auto pc = mPolicy.createPointerController(
867 PointerControllerInterface::ControllerType::STYLUS);
868 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
869 pc->setDisplayViewport(*viewport);
870 }
871 return pc;
872 };
873 return ConstructorDelegate(std::move(ctor));
874}
875
Arpit Singh7918c822024-11-20 11:28:44 +0000876// --- PointerChoreographer::PointerChoreographerDisplayInfoListener ---
877
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000878void PointerChoreographer::PointerChoreographerDisplayInfoListener::onWindowInfosChanged(
879 const gui::WindowInfosUpdate& windowInfosUpdate) {
Arpit Singh7918c822024-11-20 11:28:44 +0000880 std::scoped_lock _l(mLock);
Arpit Singh420d0742024-04-04 11:54:20 +0000881 if (mPointerChoreographer == nullptr) {
882 return;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000883 }
Arpit Singh420d0742024-04-04 11:54:20 +0000884 auto newPrivacySensitiveDisplays =
885 getPrivacySensitiveDisplaysFromWindowInfos(windowInfosUpdate.windowInfos);
886 if (newPrivacySensitiveDisplays != mPrivacySensitiveDisplays) {
887 mPrivacySensitiveDisplays = std::move(newPrivacySensitiveDisplays);
Arpit Singh7918c822024-11-20 11:28:44 +0000888 // PointerChoreographer uses Listener's lock.
889 base::ScopedLockAssertion assumeLocked(mPointerChoreographer->getLock());
890 mPointerChoreographer->onPrivacySensitiveDisplaysChangedLocked(mPrivacySensitiveDisplays);
Arpit Singh420d0742024-04-04 11:54:20 +0000891 }
892}
893
Arpit Singh7918c822024-11-20 11:28:44 +0000894void PointerChoreographer::PointerChoreographerDisplayInfoListener::setInitialDisplayInfosLocked(
Arpit Singh420d0742024-04-04 11:54:20 +0000895 const std::vector<gui::WindowInfo>& windowInfos) {
Arpit Singh420d0742024-04-04 11:54:20 +0000896 mPrivacySensitiveDisplays = getPrivacySensitiveDisplaysFromWindowInfos(windowInfos);
897}
898
899std::unordered_set<ui::LogicalDisplayId /*displayId*/>
Arpit Singh7918c822024-11-20 11:28:44 +0000900PointerChoreographer::PointerChoreographerDisplayInfoListener::getPrivacySensitiveDisplaysLocked() {
Arpit Singh420d0742024-04-04 11:54:20 +0000901 return mPrivacySensitiveDisplays;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000902}
903
904void PointerChoreographer::PointerChoreographerDisplayInfoListener::
905 onPointerChoreographerDestroyed() {
Arpit Singh7918c822024-11-20 11:28:44 +0000906 std::scoped_lock _l(mLock);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000907 mPointerChoreographer = nullptr;
908}
909
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000910} // namespace android