blob: 1585978a989a45f03581f2f7493934d1682d67a4 [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
168void PointerChoreographer::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
169 mNextListener.notify(args);
170}
171
172void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) {
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000173 fadeMouseCursorOnKeyPress(args);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000174 mNextListener.notify(args);
175}
176
177void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900178 NotifyMotionArgs newArgs = processMotion(args);
179
180 mNextListener.notify(newArgs);
181}
182
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000183void PointerChoreographer::fadeMouseCursorOnKeyPress(const android::NotifyKeyArgs& args) {
184 if (args.action == AKEY_EVENT_ACTION_UP || isMetaKey(args.keyCode)) {
185 return;
186 }
187 // Meta state for these keys is ignored for dismissing cursor while typing
188 constexpr static int32_t ALLOW_FADING_META_STATE_MASK = AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON |
189 AMETA_SCROLL_LOCK_ON | AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON | AMETA_SHIFT_ON;
190 if (args.metaState & ~ALLOW_FADING_META_STATE_MASK) {
191 // Do not fade if any other meta state is active
192 return;
193 }
194 if (!mPolicy.isInputMethodConnectionActive()) {
195 return;
196 }
197
198 std::scoped_lock _l(mLock);
199 ui::LogicalDisplayId targetDisplay = args.displayId;
200 if (targetDisplay == ui::LogicalDisplayId::INVALID) {
201 targetDisplay = mCurrentFocusedDisplay;
202 }
203 auto it = mMousePointersByDisplay.find(targetDisplay);
204 if (it != mMousePointersByDisplay.end()) {
Arpit Singh849beb42024-06-06 07:14:17 +0000205 mPolicy.notifyMouseCursorFadedOnTyping();
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000206 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
207 }
208}
209
Byoungho Jungda10dd32023-10-06 17:03:45 +0900210NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) {
211 std::scoped_lock _l(mLock);
212
213 if (isFromMouse(args)) {
214 return processMouseEventLocked(args);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900215 } else if (isFromTouchpad(args)) {
216 return processTouchpadEventLocked(args);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000217 } else if (isFromDrawingTablet(args)) {
218 processDrawingTabletEventLocked(args);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900219 } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) {
220 processStylusHoverEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900221 } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900222 processTouchscreenAndStylusEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900223 }
224 return args;
225}
226
227NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) {
228 if (args.getPointerCount() != 1) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000229 LOG(FATAL) << "Only mouse events with a single pointer are currently supported: "
230 << args.dump();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900231 }
232
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000233 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000234 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900235 NotifyMotionArgs newArgs(args);
236 newArgs.displayId = displayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900237
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900238 if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) {
239 // This is an absolute mouse device that knows about the location of the cursor on the
240 // display, so set the cursor position to the specified location.
241 const auto [x, y] = pc.getPosition();
242 const float deltaX = args.xCursorPosition - x;
243 const float deltaY = args.yCursorPosition - y;
244 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
245 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
246 pc.setPosition(args.xCursorPosition, args.yCursorPosition);
247 } else {
248 // This is a relative mouse, so move the cursor by the specified amount.
249 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
250 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
251 pc.move(deltaX, deltaY);
252 const auto [x, y] = pc.getPosition();
253 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
254 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
255 newArgs.xCursorPosition = x;
256 newArgs.yCursorPosition = y;
257 }
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000258 if (canUnfadeOnDisplay(displayId)) {
259 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
260 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900261 return newArgs;
262}
263
Byoungho Jungee6268f2023-10-30 17:27:26 +0900264NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000265 mMouseDevices.emplace(args.deviceId);
Prabir Pradhan990d8712024-03-05 00:31:36 +0000266 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900267
268 NotifyMotionArgs newArgs(args);
269 newArgs.displayId = displayId;
270 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
271 // This is a movement of the mouse pointer.
272 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
273 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
274 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000275 if (canUnfadeOnDisplay(displayId)) {
276 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
277 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900278
279 const auto [x, y] = pc.getPosition();
280 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
281 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
282 newArgs.xCursorPosition = x;
283 newArgs.yCursorPosition = y;
284 } else {
285 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000286 if (canUnfadeOnDisplay(displayId)) {
287 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
288 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900289
290 const auto [x, y] = pc.getPosition();
291 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
292 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
293 args.pointerCoords[i].getX() + x);
294 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
295 args.pointerCoords[i].getY() + y);
296 }
297 newArgs.xCursorPosition = x;
298 newArgs.yCursorPosition = y;
299 }
300 return newArgs;
301}
302
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000303void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700304 if (args.displayId == ui::LogicalDisplayId::INVALID) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000305 return;
306 }
307
308 if (args.getPointerCount() != 1) {
309 LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: "
310 << args.dump();
311 }
312
313 // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000314 auto [it, controllerAdded] =
315 mDrawingTabletPointersByDevice.try_emplace(args.deviceId,
316 getMouseControllerConstructor(
317 args.displayId));
318 if (controllerAdded) {
319 onControllerAddedOrRemovedLocked();
320 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000321
322 PointerControllerInterface& pc = *it->second;
323
324 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
325 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
326 pc.setPosition(x, y);
327 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
328 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
329 // immediately by a DOWN event.
330 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
331 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
332 } else if (canUnfadeOnDisplay(args.displayId)) {
333 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
334 }
335}
336
Byoungho Jungda10dd32023-10-06 17:03:45 +0900337/**
338 * When screen is touched, fade the mouse pointer on that display. We only call fade for
339 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
340 * mouse device keeps moving and unfades the cursor.
341 * For touch events, we do not need to populate the cursor position.
342 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900343void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800344 if (!args.displayId.isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900345 return;
346 }
347
Byoungho Jungda10dd32023-10-06 17:03:45 +0900348 if (const auto it = mMousePointersByDisplay.find(args.displayId);
349 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
350 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
351 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900352
353 if (!mShowTouchesEnabled) {
354 return;
355 }
356
357 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000358 auto [it, controllerAdded] =
359 mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
360 if (controllerAdded) {
Arpit Singh420d0742024-04-04 11:54:20 +0000361 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000362 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900363
364 PointerControllerInterface& pc = *it->second;
365
366 const PointerCoords* coords = args.pointerCoords.data();
367 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
368 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
369 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
370 BitSet32 idBits;
Linnan Li45b321e2024-07-17 19:33:21 +0000371 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL &&
372 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900373 for (size_t i = 0; i < args.getPointerCount(); i++) {
374 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
375 continue;
376 }
377 uint32_t id = args.pointerProperties[i].id;
378 idToIndex[id] = i;
379 idBits.markBit(id);
380 }
381 }
382 // The PointerController already handles setting spots per-display, so
383 // we do not need to manually manage display changes for touch spots for now.
384 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000385}
386
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900387void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800388 if (!args.displayId.isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900389 return;
390 }
391
392 if (args.getPointerCount() != 1) {
393 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
394 << args.dump();
395 }
396
397 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
Arpit Singh420d0742024-04-04 11:54:20 +0000398 auto [it, controllerAdded] =
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900399 mStylusPointersByDevice.try_emplace(args.deviceId,
400 getStylusControllerConstructor(args.displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000401 if (controllerAdded) {
402 onControllerAddedOrRemovedLocked();
403 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900404
405 PointerControllerInterface& pc = *it->second;
406
407 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
408 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
409 pc.setPosition(x, y);
410 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000411 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
412 // immediately by a DOWN event.
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900413 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan4b36db92024-01-03 20:56:57 +0000414 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000415 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900416 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
417 }
418}
419
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000420void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
421 mNextListener.notify(args);
422}
423
424void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
425 mNextListener.notify(args);
426}
427
428void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
429 mNextListener.notify(args);
430}
431
432void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900433 processDeviceReset(args);
434
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000435 mNextListener.notify(args);
436}
437
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900438void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
439 std::scoped_lock _l(mLock);
Prabir Pradhan16788792023-11-08 21:07:21 +0000440 mTouchPointersByDevice.erase(args.deviceId);
441 mStylusPointersByDevice.erase(args.deviceId);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000442 mDrawingTabletPointersByDevice.erase(args.deviceId);
Arpit Singh420d0742024-04-04 11:54:20 +0000443 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000444}
445
Arpit Singh420d0742024-04-04 11:54:20 +0000446void PointerChoreographer::onControllerAddedOrRemovedLocked() {
Arpit Singhbd49b282024-05-23 18:02:54 +0000447 if (!com::android::input::flags::hide_pointer_indicators_for_secure_windows()) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000448 return;
449 }
Arpit Singh420d0742024-04-04 11:54:20 +0000450 bool requireListener = !mTouchPointersByDevice.empty() || !mMousePointersByDisplay.empty() ||
451 !mDrawingTabletPointersByDevice.empty() || !mStylusPointersByDevice.empty();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000452
453 if (requireListener && mWindowInfoListener == nullptr) {
454 mWindowInfoListener = sp<PointerChoreographerDisplayInfoListener>::make(this);
Arpit Singhbd49b282024-05-23 18:02:54 +0000455 mWindowInfoListener->setInitialDisplayInfos(mRegisterListener(mWindowInfoListener));
Arpit Singh420d0742024-04-04 11:54:20 +0000456 onPrivacySensitiveDisplaysChangedLocked(mWindowInfoListener->getPrivacySensitiveDisplays());
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000457 } else if (!requireListener && mWindowInfoListener != nullptr) {
Arpit Singhbd49b282024-05-23 18:02:54 +0000458 mUnregisterListener(mWindowInfoListener);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000459 mWindowInfoListener = nullptr;
Arpit Singh420d0742024-04-04 11:54:20 +0000460 } else if (requireListener && mWindowInfoListener != nullptr) {
461 // controller may have been added to an existing privacy sensitive display, we need to
462 // update all controllers again
463 onPrivacySensitiveDisplaysChangedLocked(mWindowInfoListener->getPrivacySensitiveDisplays());
464 }
465}
466
467void PointerChoreographer::onPrivacySensitiveDisplaysChangedLocked(
468 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) {
469 for (auto& [_, pc] : mTouchPointersByDevice) {
470 pc->clearSkipScreenshotFlags();
471 for (auto displayId : privacySensitiveDisplays) {
472 pc->setSkipScreenshotFlagForDisplay(displayId);
473 }
474 }
475
476 for (auto& [displayId, pc] : mMousePointersByDisplay) {
477 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
478 pc->setSkipScreenshotFlagForDisplay(displayId);
479 } else {
480 pc->clearSkipScreenshotFlags();
481 }
482 }
483
484 for (auto* pointerControllerByDevice :
485 {&mDrawingTabletPointersByDevice, &mStylusPointersByDevice}) {
486 for (auto& [_, pc] : *pointerControllerByDevice) {
487 auto displayId = pc->getDisplayId();
488 if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) {
489 pc->setSkipScreenshotFlagForDisplay(displayId);
490 } else {
491 pc->clearSkipScreenshotFlags();
492 }
493 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000494 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900495}
496
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000497void PointerChoreographer::notifyPointerCaptureChanged(
498 const NotifyPointerCaptureChangedArgs& args) {
Hiroki Sato25040232024-02-22 17:21:22 +0900499 if (args.request.isEnable()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900500 std::scoped_lock _l(mLock);
501 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
502 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
503 }
504 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000505 mNextListener.notify(args);
506}
507
Arpit Singh420d0742024-04-04 11:54:20 +0000508void PointerChoreographer::onPrivacySensitiveDisplaysChanged(
509 const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) {
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000510 std::scoped_lock _l(mLock);
Arpit Singh420d0742024-04-04 11:54:20 +0000511 onPrivacySensitiveDisplaysChangedLocked(privacySensitiveDisplays);
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000512}
513
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000514void PointerChoreographer::dump(std::string& dump) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900515 std::scoped_lock _l(mLock);
516
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000517 dump += "PointerChoreographer:\n";
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900518 dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false");
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900519 dump += StringPrintf("stylus pointer icon enabled: %s\n",
520 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900521
522 dump += INDENT "MousePointerControllers:\n";
523 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
524 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
Linnan Li13bf76a2024-05-05 19:18:02 +0800525 dump += INDENT + displayId.toString() + " : " + pointerControllerDump;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900526 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900527 dump += INDENT "TouchPointerControllers:\n";
528 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
529 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
530 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
531 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900532 dump += INDENT "StylusPointerControllers:\n";
533 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
534 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
535 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
536 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000537 dump += INDENT "DrawingTabletControllers:\n";
538 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
539 std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT);
540 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
541 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900542 dump += "\n";
543}
544
Linnan Li13bf76a2024-05-05 19:18:02 +0800545const DisplayViewport* PointerChoreographer::findViewportByIdLocked(
546 ui::LogicalDisplayId displayId) const {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900547 for (auto& viewport : mViewports) {
548 if (viewport.displayId == displayId) {
549 return &viewport;
550 }
551 }
552 return nullptr;
553}
554
Linnan Li13bf76a2024-05-05 19:18:02 +0800555ui::LogicalDisplayId PointerChoreographer::getTargetMouseDisplayLocked(
556 ui::LogicalDisplayId associatedDisplayId) const {
557 return associatedDisplayId.isValid() ? associatedDisplayId : mDefaultMouseDisplayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900558}
559
Linnan Li13bf76a2024-05-05 19:18:02 +0800560std::pair<ui::LogicalDisplayId, PointerControllerInterface&>
561PointerChoreographer::ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) {
562 const ui::LogicalDisplayId displayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900563
Prabir Pradhan990d8712024-03-05 00:31:36 +0000564 auto it = mMousePointersByDisplay.find(displayId);
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000565 if (it == mMousePointersByDisplay.end()) {
566 it = mMousePointersByDisplay.emplace(displayId, getMouseControllerConstructor(displayId))
567 .first;
Arpit Singh420d0742024-04-04 11:54:20 +0000568 onControllerAddedOrRemovedLocked();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000569 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900570
571 return {displayId, *it->second};
572}
573
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900574InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000575 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
576 [deviceId](const auto& info) { return info.getId() == deviceId; });
577 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900578}
579
Linnan Li13bf76a2024-05-05 19:18:02 +0800580bool PointerChoreographer::canUnfadeOnDisplay(ui::LogicalDisplayId displayId) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000581 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
582}
583
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000584PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() {
Linnan Li13bf76a2024-05-05 19:18:02 +0800585 std::set<ui::LogicalDisplayId /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900586 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900587 std::set<DeviceId> stylusDevicesToKeep;
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000588 std::set<DeviceId> drawingTabletDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900589
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000590 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
591 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900592 for (const auto& info : mInputDeviceInfos) {
Linnan Li48f80da2024-04-22 18:38:16 +0000593 if (!info.isEnabled()) {
594 // If device is disabled, we should not keep it, and should not show pointer for
595 // disabled mouse device.
596 continue;
597 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900598 const uint32_t sources = info.getSources();
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000599 const bool isKnownMouse = mMouseDevices.count(info.getId()) != 0;
600
601 if (isMouseOrTouchpad(sources) || isKnownMouse) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800602 const ui::LogicalDisplayId displayId =
603 getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000604 mouseDisplaysToKeep.insert(displayId);
605 // For mice, show the cursor immediately when the device is first connected or
606 // when it moves to a new display.
607 auto [mousePointerIt, isNewMousePointer] =
608 mMousePointersByDisplay.try_emplace(displayId,
609 getMouseControllerConstructor(displayId));
Arpit Singh420d0742024-04-04 11:54:20 +0000610 if (isNewMousePointer) {
611 onControllerAddedOrRemovedLocked();
612 }
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000613
Prabir Pradhan5a31d3c2024-03-29 20:23:22 +0000614 mMouseDevices.emplace(info.getId());
615 if ((!isKnownMouse || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000616 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
617 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900618 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900619 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800620 info.getAssociatedDisplayId().isValid()) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900621 touchDevicesToKeep.insert(info.getId());
622 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900623 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800624 info.getAssociatedDisplayId().isValid()) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900625 stylusDevicesToKeep.insert(info.getId());
626 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000627 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) &&
Linnan Li13bf76a2024-05-05 19:18:02 +0800628 info.getAssociatedDisplayId().isValid()) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000629 drawingTabletDevicesToKeep.insert(info.getId());
630 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900631 }
632
633 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000634 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000635 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900636 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900637 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000638 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900639 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900640 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000641 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900642 });
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000643 std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) {
644 return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end();
645 });
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000646 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(mLock) {
647 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
648 [id](const auto& info) { return info.getId() == id; }) ==
649 mInputDeviceInfos.end();
650 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900651
Arpit Singh420d0742024-04-04 11:54:20 +0000652 onControllerAddedOrRemovedLocked();
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000653
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000654 // Check if we need to notify the policy if there's a change on the pointer display ID.
655 return calculatePointerDisplayChangeToNotify();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900656}
657
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000658PointerChoreographer::PointerDisplayChange
659PointerChoreographer::calculatePointerDisplayChangeToNotify() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700660 ui::LogicalDisplayId displayIdToNotify = ui::LogicalDisplayId::INVALID;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900661 FloatPoint cursorPosition = {0, 0};
662 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
663 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000664 const auto& pointerController = it->second;
665 // Use the displayId from the pointerController, because it accurately reflects whether
666 // the viewport has been added for that display. Otherwise, we would have to check if
667 // the viewport exists separately.
668 displayIdToNotify = pointerController->getDisplayId();
669 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900670 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900671 if (mNotifiedPointerDisplayId == displayIdToNotify) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000672 return {};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900673 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900674 mNotifiedPointerDisplayId = displayIdToNotify;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000675 return {{displayIdToNotify, cursorPosition}};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900676}
677
Linnan Li13bf76a2024-05-05 19:18:02 +0800678void PointerChoreographer::setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000679 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900680
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000681 { // acquire lock
682 std::scoped_lock _l(mLock);
683
684 mDefaultMouseDisplayId = displayId;
685 pointerDisplayChange = updatePointerControllersLocked();
686 } // release lock
687
688 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900689}
690
691void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000692 PointerDisplayChange pointerDisplayChange;
693
694 { // acquire lock
695 std::scoped_lock _l(mLock);
696 for (const auto& viewport : viewports) {
Linnan Li13bf76a2024-05-05 19:18:02 +0800697 const ui::LogicalDisplayId displayId = viewport.displayId;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000698 if (const auto it = mMousePointersByDisplay.find(displayId);
699 it != mMousePointersByDisplay.end()) {
700 it->second->setDisplayViewport(viewport);
701 }
702 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
703 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
704 if (info && info->getAssociatedDisplayId() == displayId) {
705 stylusPointerController->setDisplayViewport(viewport);
706 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900707 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000708 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
709 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
710 if (info && info->getAssociatedDisplayId() == displayId) {
711 drawingTabletController->setDisplayViewport(viewport);
712 }
713 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900714 }
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000715 mViewports = viewports;
716 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
717 } // release lock
718
719 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900720}
721
722std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
Linnan Li13bf76a2024-05-05 19:18:02 +0800723 ui::LogicalDisplayId associatedDisplayId) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900724 std::scoped_lock _l(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800725 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900726 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
727 return *viewport;
728 }
729 return std::nullopt;
730}
731
Linnan Li13bf76a2024-05-05 19:18:02 +0800732FloatPoint PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900733 std::scoped_lock _l(mLock);
Linnan Li13bf76a2024-05-05 19:18:02 +0800734 const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900735 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
736 it != mMousePointersByDisplay.end()) {
737 return it->second->getPosition();
738 }
739 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000740}
741
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900742void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000743 PointerDisplayChange pointerDisplayChange;
744
745 { // acquire lock
746 std::scoped_lock _l(mLock);
747 if (mShowTouchesEnabled == enabled) {
748 return;
749 }
750 mShowTouchesEnabled = enabled;
751 pointerDisplayChange = updatePointerControllersLocked();
752 } // release lock
753
754 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900755}
756
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900757void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000758 PointerDisplayChange pointerDisplayChange;
759
760 { // acquire lock
761 std::scoped_lock _l(mLock);
762 if (mStylusPointerIconEnabled == enabled) {
763 return;
764 }
765 mStylusPointerIconEnabled = enabled;
766 pointerDisplayChange = updatePointerControllersLocked();
767 } // release lock
768
769 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900770}
771
Byoungho Jung99326452023-11-03 20:19:17 +0900772bool PointerChoreographer::setPointerIcon(
Linnan Li13bf76a2024-05-05 19:18:02 +0800773 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
774 ui::LogicalDisplayId displayId, DeviceId deviceId) {
Byoungho Jung99326452023-11-03 20:19:17 +0900775 std::scoped_lock _l(mLock);
776 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000777 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900778 return false;
779 }
780 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
781 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000782 LOG(WARNING) << "No input device info found for id " << deviceId
783 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900784 return false;
785 }
786 const uint32_t sources = info->getSources();
Byoungho Jung99326452023-11-03 20:19:17 +0900787
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000788 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) {
789 auto it = mDrawingTabletPointersByDevice.find(deviceId);
790 if (it != mDrawingTabletPointersByDevice.end()) {
791 setIconForController(icon, *it->second);
792 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900793 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000794 }
795 if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) {
796 auto it = mStylusPointersByDevice.find(deviceId);
797 if (it != mStylusPointersByDevice.end()) {
798 setIconForController(icon, *it->second);
799 return true;
800 }
801 }
802 if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
803 auto it = mMousePointersByDisplay.find(displayId);
804 if (it != mMousePointersByDisplay.end()) {
805 setIconForController(icon, *it->second);
806 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900807 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000808 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
809 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900810 return false;
811 }
Byoungho Jung99326452023-11-03 20:19:17 +0900812 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000813 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId
814 << ".";
815 return false;
Byoungho Jung99326452023-11-03 20:19:17 +0900816}
817
Linnan Li13bf76a2024-05-05 19:18:02 +0800818void PointerChoreographer::setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) {
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000819 std::scoped_lock lock(mLock);
820 if (visible) {
821 mDisplaysWithPointersHidden.erase(displayId);
822 // We do not unfade the icons here, because we don't know when the last event happened.
823 return;
824 }
825
826 mDisplaysWithPointersHidden.emplace(displayId);
827
828 // Hide any icons that are currently visible on the display.
829 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
830 const auto& [_, controller] = *it;
831 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
832 }
833 for (const auto& [_, controller] : mStylusPointersByDevice) {
834 if (controller->getDisplayId() == displayId) {
835 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
836 }
837 }
838}
839
Arpit Singhb65e2bd2024-06-03 09:48:16 +0000840void PointerChoreographer::setFocusedDisplay(ui::LogicalDisplayId displayId) {
841 std::scoped_lock lock(mLock);
842 mCurrentFocusedDisplay = displayId;
843}
844
Prabir Pradhan19767602023-11-03 16:53:31 +0000845PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800846 ui::LogicalDisplayId displayId) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000847 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
848 [this, displayId]() REQUIRES(mLock) {
849 auto pc = mPolicy.createPointerController(
850 PointerControllerInterface::ControllerType::MOUSE);
851 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
852 pc->setDisplayViewport(*viewport);
853 }
854 return pc;
855 };
856 return ConstructorDelegate(std::move(ctor));
857}
858
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900859PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
Linnan Li13bf76a2024-05-05 19:18:02 +0800860 ui::LogicalDisplayId displayId) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900861 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
862 [this, displayId]() REQUIRES(mLock) {
863 auto pc = mPolicy.createPointerController(
864 PointerControllerInterface::ControllerType::STYLUS);
865 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
866 pc->setDisplayViewport(*viewport);
867 }
868 return pc;
869 };
870 return ConstructorDelegate(std::move(ctor));
871}
872
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000873void PointerChoreographer::PointerChoreographerDisplayInfoListener::onWindowInfosChanged(
874 const gui::WindowInfosUpdate& windowInfosUpdate) {
875 std::scoped_lock _l(mListenerLock);
Arpit Singh420d0742024-04-04 11:54:20 +0000876 if (mPointerChoreographer == nullptr) {
877 return;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000878 }
Arpit Singh420d0742024-04-04 11:54:20 +0000879 auto newPrivacySensitiveDisplays =
880 getPrivacySensitiveDisplaysFromWindowInfos(windowInfosUpdate.windowInfos);
881 if (newPrivacySensitiveDisplays != mPrivacySensitiveDisplays) {
882 mPrivacySensitiveDisplays = std::move(newPrivacySensitiveDisplays);
883 mPointerChoreographer->onPrivacySensitiveDisplaysChanged(mPrivacySensitiveDisplays);
884 }
885}
886
887void PointerChoreographer::PointerChoreographerDisplayInfoListener::setInitialDisplayInfos(
888 const std::vector<gui::WindowInfo>& windowInfos) {
889 std::scoped_lock _l(mListenerLock);
890 mPrivacySensitiveDisplays = getPrivacySensitiveDisplaysFromWindowInfos(windowInfos);
891}
892
893std::unordered_set<ui::LogicalDisplayId /*displayId*/>
894PointerChoreographer::PointerChoreographerDisplayInfoListener::getPrivacySensitiveDisplays() {
895 std::scoped_lock _l(mListenerLock);
896 return mPrivacySensitiveDisplays;
Arpit Singh4b6ad2d2024-04-04 11:54:20 +0000897}
898
899void PointerChoreographer::PointerChoreographerDisplayInfoListener::
900 onPointerChoreographerDestroyed() {
901 std::scoped_lock _l(mListenerLock);
902 mPointerChoreographer = nullptr;
903}
904
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000905} // namespace android