blob: 397fedac4c0ed2e88e4c9be943dad0c4f84796cb [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 Singhbd49b282024-05-23 18:02:54 +0000142 mRegisterListener(registerListener),
143 mUnregisterListener(unregisterListener) {}
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000144
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000145PointerChoreographer::~PointerChoreographer() {
146 std::scoped_lock _l(mLock);
147 if (mWindowInfoListener == nullptr) {
148 return;
149 }
150 mWindowInfoListener->onPointerChoreographerDestroyed();
Arpit Singhbd49b282024-05-23 18:02:54 +0000151 mUnregisterListener(mWindowInfoListener);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000152}
153
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000154void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000155 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900156
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000157 { // acquire lock
158 std::scoped_lock _l(mLock);
159
160 mInputDeviceInfos = args.inputDeviceInfos;
161 pointerDisplayChange = updatePointerControllersLocked();
162 } // release lock
163
164 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000165 mNextListener.notify(args);
166}
167
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000168void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) {
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000169 fadeMouseCursorOnKeyPress(args);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000170 mNextListener.notify(args);
171}
172
173void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900174 NotifyMotionArgs newArgs = processMotion(args);
175
176 mNextListener.notify(newArgs);
177}
178
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000179void PointerChoreographer::fadeMouseCursorOnKeyPress(const android::NotifyKeyArgs& args) {
180 if (args.action == AKEY_EVENT_ACTION_UP || isMetaKey(args.keyCode)) {
181 return;
182 }
183 // Meta state for these keys is ignored for dismissing cursor while typing
184 constexpr static int32_t ALLOW_FADING_META_STATE_MASK = AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON |
185 AMETA_SCROLL_LOCK_ON | AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON | AMETA_SHIFT_ON;
186 if (args.metaState & ~ALLOW_FADING_META_STATE_MASK) {
187 // Do not fade if any other meta state is active
188 return;
189 }
190 if (!mPolicy.isInputMethodConnectionActive()) {
191 return;
192 }
193
194 std::scoped_lock _l(mLock);
195 ui::LogicalDisplayId targetDisplay = args.displayId;
196 if (targetDisplay == ui::LogicalDisplayId::INVALID) {
197 targetDisplay = mCurrentFocusedDisplay;
198 }
199 auto it = mMousePointersByDisplay.find(targetDisplay);
200 if (it != mMousePointersByDisplay.end()) {
Arpit Singh849beb42024-06-06 07:14:17 +0000201 mPolicy.notifyMouseCursorFadedOnTyping();
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000202 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
203 }
204}
205
Byoungho Jungda10dd32023-10-06 17:03:45 +0900206NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) {
207 std::scoped_lock _l(mLock);
208
209 if (isFromMouse(args)) {
210 return processMouseEventLocked(args);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900211 } else if (isFromTouchpad(args)) {
212 return processTouchpadEventLocked(args);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000213 } else if (isFromDrawingTablet(args)) {
214 processDrawingTabletEventLocked(args);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900215 } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) {
216 processStylusHoverEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900217 } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900218 processTouchscreenAndStylusEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900219 }
220 return args;
221}
222
223NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) {
224 if (args.getPointerCount() != 1) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000225 LOG(FATAL) << "Only mouse events with a single pointer are currently supported: "
226 << args.dump();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900227 }
228
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000229 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000230 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900231 NotifyMotionArgs newArgs(args);
232 newArgs.displayId = displayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900233
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900234 if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) {
235 // This is an absolute mouse device that knows about the location of the cursor on the
236 // display, so set the cursor position to the specified location.
237 const auto [x, y] = pc.getPosition();
238 const float deltaX = args.xCursorPosition - x;
239 const float deltaY = args.yCursorPosition - y;
240 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
241 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
242 pc.setPosition(args.xCursorPosition, args.yCursorPosition);
243 } else {
244 // This is a relative mouse, so move the cursor by the specified amount.
245 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
246 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
247 pc.move(deltaX, deltaY);
248 const auto [x, y] = pc.getPosition();
249 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
250 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
251 newArgs.xCursorPosition = x;
252 newArgs.yCursorPosition = y;
253 }
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000254 if (canUnfadeOnDisplay(displayId)) {
255 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
256 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900257 return newArgs;
258}
259
Byoungho Jungee6268f2023-10-30 17:27:26 +0900260NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000261 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000262 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900263
264 NotifyMotionArgs newArgs(args);
265 newArgs.displayId = displayId;
266 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
267 // This is a movement of the mouse pointer.
268 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
269 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
270 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000271 if (canUnfadeOnDisplay(displayId)) {
272 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
273 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900274
275 const auto [x, y] = pc.getPosition();
276 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
277 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
278 newArgs.xCursorPosition = x;
279 newArgs.yCursorPosition = y;
280 } else {
281 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000282 if (canUnfadeOnDisplay(displayId)) {
283 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
284 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900285
286 const auto [x, y] = pc.getPosition();
287 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
288 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
289 args.pointerCoords[i].getX() + x);
290 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
291 args.pointerCoords[i].getY() + y);
292 }
293 newArgs.xCursorPosition = x;
294 newArgs.yCursorPosition = y;
295 }
296 return newArgs;
297}
298
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000299void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700300 if (args.displayId == ui::LogicalDisplayId::INVALID) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000301 return;
302 }
303
304 if (args.getPointerCount() != 1) {
305 LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: "
306 << args.dump();
307 }
308
309 // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000310 auto [it, controllerAdded] =
311 mDrawingTabletPointersByDevice.try_emplace(args.deviceId,
312 getMouseControllerConstructor(
313 args.displayId));
314 if (controllerAdded) {
315 onControllerAddedOrRemovedLocked();
316 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000317
318 PointerControllerInterface& pc = *it->second;
319
320 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
321 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
322 pc.setPosition(x, y);
323 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
324 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
325 // immediately by a DOWN event.
326 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
327 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
328 } else if (canUnfadeOnDisplay(args.displayId)) {
329 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
330 }
331}
332
Byoungho Jungda10dd32023-10-06 17:03:45 +0900333/**
334 * When screen is touched, fade the mouse pointer on that display. We only call fade for
335 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
336 * mouse device keeps moving and unfades the cursor.
337 * For touch events, we do not need to populate the cursor position.
338 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900339void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800340 if (!args.displayId.isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900341 return;
342 }
343
Byoungho Jungda10dd32023-10-06 17:03:45 +0900344 if (const auto it = mMousePointersByDisplay.find(args.displayId);
345 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
346 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
347 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900348
349 if (!mShowTouchesEnabled) {
350 return;
351 }
352
353 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000354 auto [it, controllerAdded] =
355 mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
356 if (controllerAdded) {
Arpit Singh420d0742024-04-04 11:54:20 +0000357 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000358 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900359
360 PointerControllerInterface& pc = *it->second;
361
362 const PointerCoords* coords = args.pointerCoords.data();
363 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
364 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
365 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
366 BitSet32 idBits;
Linnan Li45b321e2024-07-17 19:33:21 +0000367 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL &&
368 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900369 for (size_t i = 0; i < args.getPointerCount(); i++) {
370 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
371 continue;
372 }
373 uint32_t id = args.pointerProperties[i].id;
374 idToIndex[id] = i;
375 idBits.markBit(id);
376 }
377 }
378 // The PointerController already handles setting spots per-display, so
379 // we do not need to manually manage display changes for touch spots for now.
380 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000381}
382
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900383void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800384 if (!args.displayId.isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900385 return;
386 }
387
388 if (args.getPointerCount() != 1) {
389 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
390 << args.dump();
391 }
392
393 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000394 auto [it, controllerAdded] =
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900395 mStylusPointersByDevice.try_emplace(args.deviceId,
396 getStylusControllerConstructor(args.displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000397 if (controllerAdded) {
398 onControllerAddedOrRemovedLocked();
399 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900400
401 PointerControllerInterface& pc = *it->second;
402
403 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
404 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
405 pc.setPosition(x, y);
406 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000407 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
408 // immediately by a DOWN event.
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900409 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan4b36db92024-01-03 20:56:57 +0000410 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000411 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900412 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
413 }
414}
415
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000416void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
417 mNextListener.notify(args);
418}
419
420void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
421 mNextListener.notify(args);
422}
423
424void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
425 mNextListener.notify(args);
426}
427
428void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900429 processDeviceReset(args);
430
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000431 mNextListener.notify(args);
432}
433
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900434void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
435 std::scoped_lock _l(mLock);
Prabir Pradhan16788792023-11-08 21:07:21 +0000436 mTouchPointersByDevice.erase(args.deviceId);
437 mStylusPointersByDevice.erase(args.deviceId);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000438 mDrawingTabletPointersByDevice.erase(args.deviceId);
Arpit Singh420d0742024-04-04 11:54:20 +0000439 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000440}
441
Arpit Singh420d0742024-04-04 11:54:20 +0000442void PointerChoreographer::onControllerAddedOrRemovedLocked() {
Arpit Singhbd49b282024-05-23 18:02:54 +0000443 if (!com::android::input::flags::hide_pointer_indicators_for_secure_windows()) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000444 return;
445 }
Arpit Singh420d0742024-04-04 11:54:20 +0000446 bool requireListener = !mTouchPointersByDevice.empty() || !mMousePointersByDisplay.empty() ||
447 !mDrawingTabletPointersByDevice.empty() || !mStylusPointersByDevice.empty();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000448
449 if (requireListener && mWindowInfoListener == nullptr) {
450 mWindowInfoListener = sp<PointerChoreographerDisplayInfoListener>::make(this);
Arpit Singhbd49b282024-05-23 18:02:54 +0000451 mWindowInfoListener->setInitialDisplayInfos(mRegisterListener(mWindowInfoListener));
Arpit Singh420d0742024-04-04 11:54:20 +0000452 onPrivacySensitiveDisplaysChangedLocked(mWindowInfoListener->getPrivacySensitiveDisplays());
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000453 } else if (!requireListener && mWindowInfoListener != nullptr) {
Arpit Singhbd49b282024-05-23 18:02:54 +0000454 mUnregisterListener(mWindowInfoListener);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000455 mWindowInfoListener = nullptr;
Arpit Singh420d0742024-04-04 11:54:20 +0000456 } else if (requireListener && mWindowInfoListener != nullptr) {
457 // controller may have been added to an existing privacy sensitive display, we need to
458 // update all controllers again
459 onPrivacySensitiveDisplaysChangedLocked(mWindowInfoListener->getPrivacySensitiveDisplays());
460 }
461}
462
463void PointerChoreographer::onPrivacySensitiveDisplaysChangedLocked(
464 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) {
465 for (auto& [_, pc] : mTouchPointersByDevice) {
466 pc->clearSkipScreenshotFlags();
467 for (auto displayId : privacySensitiveDisplays) {
468 pc->setSkipScreenshotFlagForDisplay(displayId);
469 }
470 }
471
472 for (auto& [displayId, pc] : mMousePointersByDisplay) {
473 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
474 pc->setSkipScreenshotFlagForDisplay(displayId);
475 } else {
476 pc->clearSkipScreenshotFlags();
477 }
478 }
479
480 for (auto* pointerControllerByDevice :
481 {&mDrawingTabletPointersByDevice, &mStylusPointersByDevice}) {
482 for (auto& [_, pc] : *pointerControllerByDevice) {
483 auto displayId = pc->getDisplayId();
484 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
485 pc->setSkipScreenshotFlagForDisplay(displayId);
486 } else {
487 pc->clearSkipScreenshotFlags();
488 }
489 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000490 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900491}
492
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000493void PointerChoreographer::notifyPointerCaptureChanged(
494 const NotifyPointerCaptureChangedArgs& args) {
Hiroki Sato25040232024-02-22 17:21:22 +0900495 if (args.request.isEnable()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900496 std::scoped_lock _l(mLock);
497 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
498 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
499 }
500 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000501 mNextListener.notify(args);
502}
503
Arpit Singh420d0742024-04-04 11:54:20 +0000504void PointerChoreographer::onPrivacySensitiveDisplaysChanged(
505 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000506 std::scoped_lock _l(mLock);
Arpit Singh420d0742024-04-04 11:54:20 +0000507 onPrivacySensitiveDisplaysChangedLocked(privacySensitiveDisplays);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000508}
509
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000510void PointerChoreographer::dump(std::string& dump) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900511 std::scoped_lock _l(mLock);
512
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000513 dump += "PointerChoreographer:\n";
Harry Cuttsebd418a2024-08-16 15:52:24 +0000514 dump += StringPrintf(INDENT "Show Touches Enabled: %s\n",
515 mShowTouchesEnabled ? "true" : "false");
516 dump += StringPrintf(INDENT "Stylus PointerIcon Enabled: %s\n",
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900517 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900518
519 dump += INDENT "MousePointerControllers:\n";
520 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
521 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
Linnan Li13bf76a2024-05-05 19:18:02 +0800522 dump += INDENT + displayId.toString() + " : " + pointerControllerDump;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900523 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900524 dump += INDENT "TouchPointerControllers:\n";
525 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
526 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
527 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
528 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900529 dump += INDENT "StylusPointerControllers:\n";
530 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
531 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
532 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
533 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000534 dump += INDENT "DrawingTabletControllers:\n";
535 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
536 std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT);
537 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
538 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900539 dump += "\n";
540}
541
Linnan Li13bf76a2024-05-05 19:18:02 +0800542const DisplayViewport* PointerChoreographer::findViewportByIdLocked(
543 ui::LogicalDisplayId displayId) const {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900544 for (auto& viewport : mViewports) {
545 if (viewport.displayId == displayId) {
546 return &viewport;
547 }
548 }
549 return nullptr;
550}
551
Linnan Li13bf76a2024-05-05 19:18:02 +0800552ui::LogicalDisplayId PointerChoreographer::getTargetMouseDisplayLocked(
553 ui::LogicalDisplayId associatedDisplayId) const {
554 return associatedDisplayId.isValid() ? associatedDisplayId : mDefaultMouseDisplayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900555}
556
Linnan Li13bf76a2024-05-05 19:18:02 +0800557std::pair<ui::LogicalDisplayId, PointerControllerInterface&>
558PointerChoreographer::ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) {
559 const ui::LogicalDisplayId displayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900560
Prabir Pradhan990d8712024-03-05 00:31:36 +0000561 auto it = mMousePointersByDisplay.find(displayId);
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000562 if (it == mMousePointersByDisplay.end()) {
563 it = mMousePointersByDisplay.emplace(displayId, getMouseControllerConstructor(displayId))
564 .first;
Arpit Singh420d0742024-04-04 11:54:20 +0000565 onControllerAddedOrRemovedLocked();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000566 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900567
568 return {displayId, *it->second};
569}
570
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900571InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000572 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
573 [deviceId](const auto& info) { return info.getId() == deviceId; });
574 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900575}
576
Linnan Li13bf76a2024-05-05 19:18:02 +0800577bool PointerChoreographer::canUnfadeOnDisplay(ui::LogicalDisplayId displayId) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000578 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
579}
580
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000581PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() {
Linnan Li13bf76a2024-05-05 19:18:02 +0800582 std::set<ui::LogicalDisplayId /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900583 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900584 std::set<DeviceId> stylusDevicesToKeep;
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000585 std::set<DeviceId> drawingTabletDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900586
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000587 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
588 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900589 for (const auto& info : mInputDeviceInfos) {
Linnan Li48f80da2024-04-22 18:38:16 +0000590 if (!info.isEnabled()) {
591 // If device is disabled, we should not keep it, and should not show pointer for
592 // disabled mouse device.
593 continue;
594 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900595 const uint32_t sources = info.getSources();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000596 const bool isKnownMouse = mMouseDevices.count(info.getId()) != 0;
597
598 if (isMouseOrTouchpad(sources) || isKnownMouse) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800599 const ui::LogicalDisplayId displayId =
600 getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000601 mouseDisplaysToKeep.insert(displayId);
602 // For mice, show the cursor immediately when the device is first connected or
603 // when it moves to a new display.
604 auto [mousePointerIt, isNewMousePointer] =
605 mMousePointersByDisplay.try_emplace(displayId,
606 getMouseControllerConstructor(displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000607 if (isNewMousePointer) {
608 onControllerAddedOrRemovedLocked();
609 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000610
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000611 mMouseDevices.emplace(info.getId());
612 if ((!isKnownMouse || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000613 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
614 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900615 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900616 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800617 info.getAssociatedDisplayId().isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900618 touchDevicesToKeep.insert(info.getId());
619 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900620 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800621 info.getAssociatedDisplayId().isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900622 stylusDevicesToKeep.insert(info.getId());
623 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000624 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800625 info.getAssociatedDisplayId().isValid()) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000626 drawingTabletDevicesToKeep.insert(info.getId());
627 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900628 }
629
630 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000631 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000632 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900633 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900634 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000635 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900636 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900637 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000638 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900639 });
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000640 std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) {
641 return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end();
642 });
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000643 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(mLock) {
644 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
645 [id](const auto& info) { return info.getId() == id; }) ==
646 mInputDeviceInfos.end();
647 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900648
Arpit Singh420d0742024-04-04 11:54:20 +0000649 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000650
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000651 // Check if we need to notify the policy if there's a change on the pointer display ID.
652 return calculatePointerDisplayChangeToNotify();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900653}
654
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000655PointerChoreographer::PointerDisplayChange
656PointerChoreographer::calculatePointerDisplayChangeToNotify() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700657 ui::LogicalDisplayId displayIdToNotify = ui::LogicalDisplayId::INVALID;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900658 FloatPoint cursorPosition = {0, 0};
659 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
660 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000661 const auto& pointerController = it->second;
662 // Use the displayId from the pointerController, because it accurately reflects whether
663 // the viewport has been added for that display. Otherwise, we would have to check if
664 // the viewport exists separately.
665 displayIdToNotify = pointerController->getDisplayId();
666 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900667 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900668 if (mNotifiedPointerDisplayId == displayIdToNotify) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000669 return {};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900670 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900671 mNotifiedPointerDisplayId = displayIdToNotify;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000672 return {{displayIdToNotify, cursorPosition}};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900673}
674
Linnan Li13bf76a2024-05-05 19:18:02 +0800675void PointerChoreographer::setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000676 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900677
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000678 { // acquire lock
679 std::scoped_lock _l(mLock);
680
681 mDefaultMouseDisplayId = displayId;
682 pointerDisplayChange = updatePointerControllersLocked();
683 } // release lock
684
685 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900686}
687
688void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000689 PointerDisplayChange pointerDisplayChange;
690
691 { // acquire lock
692 std::scoped_lock _l(mLock);
693 for (const auto& viewport : viewports) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800694 const ui::LogicalDisplayId displayId = viewport.displayId;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000695 if (const auto it = mMousePointersByDisplay.find(displayId);
696 it != mMousePointersByDisplay.end()) {
697 it->second->setDisplayViewport(viewport);
698 }
699 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
700 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
701 if (info && info->getAssociatedDisplayId() == displayId) {
702 stylusPointerController->setDisplayViewport(viewport);
703 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900704 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000705 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
706 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
707 if (info && info->getAssociatedDisplayId() == displayId) {
708 drawingTabletController->setDisplayViewport(viewport);
709 }
710 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900711 }
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000712 mViewports = viewports;
713 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
714 } // release lock
715
716 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900717}
718
719std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
Linnan Li13bf76a2024-05-05 19:18:02 +0800720 ui::LogicalDisplayId associatedDisplayId) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900721 std::scoped_lock _l(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800722 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900723 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
724 return *viewport;
725 }
726 return std::nullopt;
727}
728
Linnan Li13bf76a2024-05-05 19:18:02 +0800729FloatPoint PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900730 std::scoped_lock _l(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800731 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900732 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
733 it != mMousePointersByDisplay.end()) {
734 return it->second->getPosition();
735 }
736 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000737}
738
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900739void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000740 PointerDisplayChange pointerDisplayChange;
741
742 { // acquire lock
743 std::scoped_lock _l(mLock);
744 if (mShowTouchesEnabled == enabled) {
745 return;
746 }
747 mShowTouchesEnabled = enabled;
748 pointerDisplayChange = updatePointerControllersLocked();
749 } // release lock
750
751 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900752}
753
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900754void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000755 PointerDisplayChange pointerDisplayChange;
756
757 { // acquire lock
758 std::scoped_lock _l(mLock);
759 if (mStylusPointerIconEnabled == enabled) {
760 return;
761 }
762 mStylusPointerIconEnabled = enabled;
763 pointerDisplayChange = updatePointerControllersLocked();
764 } // release lock
765
766 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900767}
768
Byoungho Jung99326452023-11-03 20:19:17 +0900769bool PointerChoreographer::setPointerIcon(
Linnan Li13bf76a2024-05-05 19:18:02 +0800770 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
771 ui::LogicalDisplayId displayId, DeviceId deviceId) {
Byoungho Jung99326452023-11-03 20:19:17 +0900772 std::scoped_lock _l(mLock);
773 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000774 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900775 return false;
776 }
777 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
778 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000779 LOG(WARNING) << "No input device info found for id " << deviceId
780 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900781 return false;
782 }
783 const uint32_t sources = info->getSources();
Byoungho Jung99326452023-11-03 20:19:17 +0900784
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000785 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) {
786 auto it = mDrawingTabletPointersByDevice.find(deviceId);
787 if (it != mDrawingTabletPointersByDevice.end()) {
788 setIconForController(icon, *it->second);
789 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900790 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000791 }
792 if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) {
793 auto it = mStylusPointersByDevice.find(deviceId);
794 if (it != mStylusPointersByDevice.end()) {
795 setIconForController(icon, *it->second);
796 return true;
797 }
798 }
799 if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
800 auto it = mMousePointersByDisplay.find(displayId);
801 if (it != mMousePointersByDisplay.end()) {
802 setIconForController(icon, *it->second);
803 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900804 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000805 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
806 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900807 return false;
808 }
Byoungho Jung99326452023-11-03 20:19:17 +0900809 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000810 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId
811 << ".";
812 return false;
Byoungho Jung99326452023-11-03 20:19:17 +0900813}
814
Linnan Li13bf76a2024-05-05 19:18:02 +0800815void PointerChoreographer::setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000816 std::scoped_lock lock(mLock);
817 if (visible) {
818 mDisplaysWithPointersHidden.erase(displayId);
819 // We do not unfade the icons here, because we don't know when the last event happened.
820 return;
821 }
822
823 mDisplaysWithPointersHidden.emplace(displayId);
824
825 // Hide any icons that are currently visible on the display.
826 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
827 const auto& [_, controller] = *it;
828 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
829 }
830 for (const auto& [_, controller] : mStylusPointersByDevice) {
831 if (controller->getDisplayId() == displayId) {
832 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
833 }
834 }
835}
836
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000837void PointerChoreographer::setFocusedDisplay(ui::LogicalDisplayId displayId) {
838 std::scoped_lock lock(mLock);
839 mCurrentFocusedDisplay = displayId;
840}
841
Prabir Pradhan19767602023-11-03 16:53:31 +0000842PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800843 ui::LogicalDisplayId displayId) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000844 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
845 [this, displayId]() REQUIRES(mLock) {
846 auto pc = mPolicy.createPointerController(
847 PointerControllerInterface::ControllerType::MOUSE);
848 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
849 pc->setDisplayViewport(*viewport);
850 }
851 return pc;
852 };
853 return ConstructorDelegate(std::move(ctor));
854}
855
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900856PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800857 ui::LogicalDisplayId displayId) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900858 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
859 [this, displayId]() REQUIRES(mLock) {
860 auto pc = mPolicy.createPointerController(
861 PointerControllerInterface::ControllerType::STYLUS);
862 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
863 pc->setDisplayViewport(*viewport);
864 }
865 return pc;
866 };
867 return ConstructorDelegate(std::move(ctor));
868}
869
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000870void PointerChoreographer::PointerChoreographerDisplayInfoListener::onWindowInfosChanged(
871 const gui::WindowInfosUpdate& windowInfosUpdate) {
872 std::scoped_lock _l(mListenerLock);
Arpit Singh420d0742024-04-04 11:54:20 +0000873 if (mPointerChoreographer == nullptr) {
874 return;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000875 }
Arpit Singh420d0742024-04-04 11:54:20 +0000876 auto newPrivacySensitiveDisplays =
877 getPrivacySensitiveDisplaysFromWindowInfos(windowInfosUpdate.windowInfos);
878 if (newPrivacySensitiveDisplays != mPrivacySensitiveDisplays) {
879 mPrivacySensitiveDisplays = std::move(newPrivacySensitiveDisplays);
880 mPointerChoreographer->onPrivacySensitiveDisplaysChanged(mPrivacySensitiveDisplays);
881 }
882}
883
884void PointerChoreographer::PointerChoreographerDisplayInfoListener::setInitialDisplayInfos(
885 const std::vector<gui::WindowInfo>& windowInfos) {
886 std::scoped_lock _l(mListenerLock);
887 mPrivacySensitiveDisplays = getPrivacySensitiveDisplaysFromWindowInfos(windowInfos);
888}
889
890std::unordered_set<ui::LogicalDisplayId /*displayId*/>
891PointerChoreographer::PointerChoreographerDisplayInfoListener::getPrivacySensitiveDisplays() {
892 std::scoped_lock _l(mListenerLock);
893 return mPrivacySensitiveDisplays;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000894}
895
896void PointerChoreographer::PointerChoreographerDisplayInfoListener::
897 onPointerChoreographerDestroyed() {
898 std::scoped_lock _l(mListenerLock);
899 mPointerChoreographer = nullptr;
900}
901
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000902} // namespace android