blob: 1aa1077d547cfc4d2a5c395ccb567fdd2fcb9357 [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>
20#include <input/PrintTools.h>
21
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000022#include "PointerChoreographer.h"
23
Byoungho Jungda10dd32023-10-06 17:03:45 +090024#define INDENT " "
25
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000026namespace android {
27
Byoungho Jungda10dd32023-10-06 17:03:45 +090028namespace {
Prabir Pradhan5a51a222024-03-05 03:54:00 +000029
Byoungho Jungda10dd32023-10-06 17:03:45 +090030bool isFromMouse(const NotifyMotionArgs& args) {
31 return isFromSource(args.source, AINPUT_SOURCE_MOUSE) &&
32 args.pointerProperties[0].toolType == ToolType::MOUSE;
33}
34
Byoungho Jungee6268f2023-10-30 17:27:26 +090035bool isFromTouchpad(const NotifyMotionArgs& args) {
36 return isFromSource(args.source, AINPUT_SOURCE_MOUSE) &&
37 args.pointerProperties[0].toolType == ToolType::FINGER;
38}
39
Prabir Pradhan4c977a42024-03-15 16:47:37 +000040bool isFromDrawingTablet(const NotifyMotionArgs& args) {
41 return isFromSource(args.source, AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS) &&
42 isStylusToolType(args.pointerProperties[0].toolType);
43}
44
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090045bool isHoverAction(int32_t action) {
46 return action == AMOTION_EVENT_ACTION_HOVER_ENTER ||
47 action == AMOTION_EVENT_ACTION_HOVER_MOVE || action == AMOTION_EVENT_ACTION_HOVER_EXIT;
48}
49
50bool isStylusHoverEvent(const NotifyMotionArgs& args) {
51 return isStylusEvent(args.source, args.pointerProperties) && isHoverAction(args.action);
52}
Prabir Pradhan5a51a222024-03-05 03:54:00 +000053
Prabir Pradhan4c977a42024-03-15 16:47:37 +000054bool isMouseOrTouchpad(uint32_t sources) {
55 // Check if this is a mouse or touchpad, but not a drawing tablet.
56 return isFromSource(sources, AINPUT_SOURCE_MOUSE_RELATIVE) ||
57 (isFromSource(sources, AINPUT_SOURCE_MOUSE) &&
58 !isFromSource(sources, AINPUT_SOURCE_STYLUS));
59}
60
Prabir Pradhan5a51a222024-03-05 03:54:00 +000061inline void notifyPointerDisplayChange(std::optional<std::tuple<int32_t, FloatPoint>> change,
62 PointerChoreographerPolicyInterface& policy) {
63 if (!change) {
64 return;
65 }
66 const auto& [displayId, cursorPosition] = *change;
67 policy.notifyPointerDisplayIdChanged(displayId, cursorPosition);
68}
69
Prabir Pradhan4c977a42024-03-15 16:47:37 +000070void setIconForController(const std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle>& icon,
71 PointerControllerInterface& controller) {
72 if (std::holds_alternative<std::unique_ptr<SpriteIcon>>(icon)) {
73 if (std::get<std::unique_ptr<SpriteIcon>>(icon) == nullptr) {
74 LOG(FATAL) << "SpriteIcon should not be null";
75 }
76 controller.setCustomPointerIcon(*std::get<std::unique_ptr<SpriteIcon>>(icon));
77 } else {
78 controller.updatePointerIcon(std::get<PointerIconStyle>(icon));
79 }
80}
81
Byoungho Jungda10dd32023-10-06 17:03:45 +090082} // namespace
83
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000084// --- PointerChoreographer ---
85
86PointerChoreographer::PointerChoreographer(InputListenerInterface& listener,
87 PointerChoreographerPolicyInterface& policy)
Prabir Pradhan5a51a222024-03-05 03:54:00 +000088 : mTouchControllerConstructor([this]() {
Prabir Pradhan16788792023-11-08 21:07:21 +000089 return mPolicy.createPointerController(
90 PointerControllerInterface::ControllerType::TOUCH);
91 }),
92 mNextListener(listener),
Byoungho Jungda10dd32023-10-06 17:03:45 +090093 mPolicy(policy),
94 mDefaultMouseDisplayId(ADISPLAY_ID_DEFAULT),
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090095 mNotifiedPointerDisplayId(ADISPLAY_ID_NONE),
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090096 mShowTouchesEnabled(false),
97 mStylusPointerIconEnabled(false) {}
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000098
99void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000100 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000102 { // acquire lock
103 std::scoped_lock _l(mLock);
104
105 mInputDeviceInfos = args.inputDeviceInfos;
106 pointerDisplayChange = updatePointerControllersLocked();
107 } // release lock
108
109 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000110 mNextListener.notify(args);
111}
112
113void PointerChoreographer::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
114 mNextListener.notify(args);
115}
116
117void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) {
118 mNextListener.notify(args);
119}
120
121void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900122 NotifyMotionArgs newArgs = processMotion(args);
123
124 mNextListener.notify(newArgs);
125}
126
127NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) {
128 std::scoped_lock _l(mLock);
129
130 if (isFromMouse(args)) {
131 return processMouseEventLocked(args);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900132 } else if (isFromTouchpad(args)) {
133 return processTouchpadEventLocked(args);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000134 } else if (isFromDrawingTablet(args)) {
135 processDrawingTabletEventLocked(args);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900136 } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) {
137 processStylusHoverEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900138 } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900139 processTouchscreenAndStylusEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900140 }
141 return args;
142}
143
144NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) {
145 if (args.getPointerCount() != 1) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000146 LOG(FATAL) << "Only mouse events with a single pointer are currently supported: "
147 << args.dump();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900148 }
149
Prabir Pradhan990d8712024-03-05 00:31:36 +0000150 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900151 NotifyMotionArgs newArgs(args);
152 newArgs.displayId = displayId;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900153
Nergi Rahardie0a4cfe2024-03-11 13:18:59 +0900154 if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) {
155 // This is an absolute mouse device that knows about the location of the cursor on the
156 // display, so set the cursor position to the specified location.
157 const auto [x, y] = pc.getPosition();
158 const float deltaX = args.xCursorPosition - x;
159 const float deltaY = args.yCursorPosition - y;
160 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX);
161 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY);
162 pc.setPosition(args.xCursorPosition, args.yCursorPosition);
163 } else {
164 // This is a relative mouse, so move the cursor by the specified amount.
165 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
166 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
167 pc.move(deltaX, deltaY);
168 const auto [x, y] = pc.getPosition();
169 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
170 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
171 newArgs.xCursorPosition = x;
172 newArgs.yCursorPosition = y;
173 }
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000174 if (canUnfadeOnDisplay(displayId)) {
175 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
176 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900177 return newArgs;
178}
179
Byoungho Jungee6268f2023-10-30 17:27:26 +0900180NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
Prabir Pradhan990d8712024-03-05 00:31:36 +0000181 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900182
183 NotifyMotionArgs newArgs(args);
184 newArgs.displayId = displayId;
185 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
186 // This is a movement of the mouse pointer.
187 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
188 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
189 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000190 if (canUnfadeOnDisplay(displayId)) {
191 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
192 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900193
194 const auto [x, y] = pc.getPosition();
195 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
196 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
197 newArgs.xCursorPosition = x;
198 newArgs.yCursorPosition = y;
199 } else {
200 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000201 if (canUnfadeOnDisplay(displayId)) {
202 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
203 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900204
205 const auto [x, y] = pc.getPosition();
206 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
207 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
208 args.pointerCoords[i].getX() + x);
209 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
210 args.pointerCoords[i].getY() + y);
211 }
212 newArgs.xCursorPosition = x;
213 newArgs.yCursorPosition = y;
214 }
215 return newArgs;
216}
217
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000218void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) {
219 if (args.displayId == ADISPLAY_ID_NONE) {
220 return;
221 }
222
223 if (args.getPointerCount() != 1) {
224 LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: "
225 << args.dump();
226 }
227
228 // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist.
229 auto [it, _] = mDrawingTabletPointersByDevice.try_emplace(args.deviceId,
230 getMouseControllerConstructor(
231 args.displayId));
232
233 PointerControllerInterface& pc = *it->second;
234
235 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
236 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
237 pc.setPosition(x, y);
238 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
239 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
240 // immediately by a DOWN event.
241 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
242 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
243 } else if (canUnfadeOnDisplay(args.displayId)) {
244 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
245 }
246}
247
Byoungho Jungda10dd32023-10-06 17:03:45 +0900248/**
249 * When screen is touched, fade the mouse pointer on that display. We only call fade for
250 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
251 * mouse device keeps moving and unfades the cursor.
252 * For touch events, we do not need to populate the cursor position.
253 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900254void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
255 if (args.displayId == ADISPLAY_ID_NONE) {
256 return;
257 }
258
Byoungho Jungda10dd32023-10-06 17:03:45 +0900259 if (const auto it = mMousePointersByDisplay.find(args.displayId);
260 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
261 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
262 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900263
264 if (!mShowTouchesEnabled) {
265 return;
266 }
267
268 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Prabir Pradhan16788792023-11-08 21:07:21 +0000269 auto [it, _] = mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900270
271 PointerControllerInterface& pc = *it->second;
272
273 const PointerCoords* coords = args.pointerCoords.data();
274 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
275 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
276 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
277 BitSet32 idBits;
278 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL) {
279 for (size_t i = 0; i < args.getPointerCount(); i++) {
280 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
281 continue;
282 }
283 uint32_t id = args.pointerProperties[i].id;
284 idToIndex[id] = i;
285 idBits.markBit(id);
286 }
287 }
288 // The PointerController already handles setting spots per-display, so
289 // we do not need to manually manage display changes for touch spots for now.
290 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000291}
292
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900293void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
294 if (args.displayId == ADISPLAY_ID_NONE) {
295 return;
296 }
297
298 if (args.getPointerCount() != 1) {
299 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
300 << args.dump();
301 }
302
303 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
304 auto [it, _] =
305 mStylusPointersByDevice.try_emplace(args.deviceId,
306 getStylusControllerConstructor(args.displayId));
307
308 PointerControllerInterface& pc = *it->second;
309
310 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
311 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
312 pc.setPosition(x, y);
313 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000314 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
315 // immediately by a DOWN event.
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900316 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan4b36db92024-01-03 20:56:57 +0000317 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000318 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900319 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
320 }
321}
322
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000323void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
324 mNextListener.notify(args);
325}
326
327void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
328 mNextListener.notify(args);
329}
330
331void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
332 mNextListener.notify(args);
333}
334
335void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900336 processDeviceReset(args);
337
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000338 mNextListener.notify(args);
339}
340
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900341void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
342 std::scoped_lock _l(mLock);
Prabir Pradhan16788792023-11-08 21:07:21 +0000343 mTouchPointersByDevice.erase(args.deviceId);
344 mStylusPointersByDevice.erase(args.deviceId);
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000345 mDrawingTabletPointersByDevice.erase(args.deviceId);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900346}
347
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000348void PointerChoreographer::notifyPointerCaptureChanged(
349 const NotifyPointerCaptureChangedArgs& args) {
Hiroki Sato25040232024-02-22 17:21:22 +0900350 if (args.request.isEnable()) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900351 std::scoped_lock _l(mLock);
352 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
353 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
354 }
355 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000356 mNextListener.notify(args);
357}
358
359void PointerChoreographer::dump(std::string& dump) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900360 std::scoped_lock _l(mLock);
361
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000362 dump += "PointerChoreographer:\n";
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900363 dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false");
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900364 dump += StringPrintf("stylus pointer icon enabled: %s\n",
365 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900366
367 dump += INDENT "MousePointerControllers:\n";
368 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
369 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
370 dump += INDENT + std::to_string(displayId) + " : " + pointerControllerDump;
371 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900372 dump += INDENT "TouchPointerControllers:\n";
373 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
374 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
375 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
376 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900377 dump += INDENT "StylusPointerControllers:\n";
378 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
379 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
380 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
381 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000382 dump += INDENT "DrawingTabletControllers:\n";
383 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
384 std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT);
385 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
386 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900387 dump += "\n";
388}
389
390const DisplayViewport* PointerChoreographer::findViewportByIdLocked(int32_t displayId) const {
391 for (auto& viewport : mViewports) {
392 if (viewport.displayId == displayId) {
393 return &viewport;
394 }
395 }
396 return nullptr;
397}
398
399int32_t PointerChoreographer::getTargetMouseDisplayLocked(int32_t associatedDisplayId) const {
400 return associatedDisplayId == ADISPLAY_ID_NONE ? mDefaultMouseDisplayId : associatedDisplayId;
401}
402
Prabir Pradhan990d8712024-03-05 00:31:36 +0000403std::pair<int32_t, PointerControllerInterface&> PointerChoreographer::ensureMouseControllerLocked(
404 int32_t associatedDisplayId) {
Byoungho Jungee6268f2023-10-30 17:27:26 +0900405 const int32_t displayId = getTargetMouseDisplayLocked(associatedDisplayId);
406
Prabir Pradhan990d8712024-03-05 00:31:36 +0000407 auto it = mMousePointersByDisplay.find(displayId);
408 LOG_ALWAYS_FATAL_IF(it == mMousePointersByDisplay.end(),
409 "There is no mouse controller created for display %d", displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900410
411 return {displayId, *it->second};
412}
413
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900414InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000415 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
416 [deviceId](const auto& info) { return info.getId() == deviceId; });
417 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900418}
419
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000420bool PointerChoreographer::canUnfadeOnDisplay(int32_t displayId) {
421 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
422}
423
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000424PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900425 std::set<int32_t /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900426 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900427 std::set<DeviceId> stylusDevicesToKeep;
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000428 std::set<DeviceId> drawingTabletDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900429
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000430 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
431 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900432 for (const auto& info : mInputDeviceInfos) {
433 const uint32_t sources = info.getSources();
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000434 if (isMouseOrTouchpad(sources)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000435 const int32_t displayId = getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
436 mouseDisplaysToKeep.insert(displayId);
437 // For mice, show the cursor immediately when the device is first connected or
438 // when it moves to a new display.
439 auto [mousePointerIt, isNewMousePointer] =
440 mMousePointersByDisplay.try_emplace(displayId,
441 getMouseControllerConstructor(displayId));
442 auto [_, isNewMouseDevice] = mMouseDevices.emplace(info.getId());
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000443 if ((isNewMouseDevice || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000444 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
445 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900446 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900447 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
448 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
449 touchDevicesToKeep.insert(info.getId());
450 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900451 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
452 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
453 stylusDevicesToKeep.insert(info.getId());
454 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000455 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) &&
456 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
457 drawingTabletDevicesToKeep.insert(info.getId());
458 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900459 }
460
461 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000462 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000463 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900464 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900465 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000466 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900467 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900468 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000469 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900470 });
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000471 std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) {
472 return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end();
473 });
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000474 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(mLock) {
475 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
476 [id](const auto& info) { return info.getId() == id; }) ==
477 mInputDeviceInfos.end();
478 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900479
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000480 // Check if we need to notify the policy if there's a change on the pointer display ID.
481 return calculatePointerDisplayChangeToNotify();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900482}
483
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000484PointerChoreographer::PointerDisplayChange
485PointerChoreographer::calculatePointerDisplayChangeToNotify() {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900486 int32_t displayIdToNotify = ADISPLAY_ID_NONE;
487 FloatPoint cursorPosition = {0, 0};
488 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
489 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000490 const auto& pointerController = it->second;
491 // Use the displayId from the pointerController, because it accurately reflects whether
492 // the viewport has been added for that display. Otherwise, we would have to check if
493 // the viewport exists separately.
494 displayIdToNotify = pointerController->getDisplayId();
495 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900496 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900497 if (mNotifiedPointerDisplayId == displayIdToNotify) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000498 return {};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900499 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900500 mNotifiedPointerDisplayId = displayIdToNotify;
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000501 return {{displayIdToNotify, cursorPosition}};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900502}
503
504void PointerChoreographer::setDefaultMouseDisplayId(int32_t displayId) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000505 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900506
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000507 { // acquire lock
508 std::scoped_lock _l(mLock);
509
510 mDefaultMouseDisplayId = displayId;
511 pointerDisplayChange = updatePointerControllersLocked();
512 } // release lock
513
514 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900515}
516
517void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000518 PointerDisplayChange pointerDisplayChange;
519
520 { // acquire lock
521 std::scoped_lock _l(mLock);
522 for (const auto& viewport : viewports) {
523 const int32_t displayId = viewport.displayId;
524 if (const auto it = mMousePointersByDisplay.find(displayId);
525 it != mMousePointersByDisplay.end()) {
526 it->second->setDisplayViewport(viewport);
527 }
528 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
529 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
530 if (info && info->getAssociatedDisplayId() == displayId) {
531 stylusPointerController->setDisplayViewport(viewport);
532 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900533 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000534 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
535 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
536 if (info && info->getAssociatedDisplayId() == displayId) {
537 drawingTabletController->setDisplayViewport(viewport);
538 }
539 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900540 }
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000541 mViewports = viewports;
542 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
543 } // release lock
544
545 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900546}
547
548std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
549 int32_t associatedDisplayId) {
550 std::scoped_lock _l(mLock);
551 const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
552 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
553 return *viewport;
554 }
555 return std::nullopt;
556}
557
558FloatPoint PointerChoreographer::getMouseCursorPosition(int32_t displayId) {
559 std::scoped_lock _l(mLock);
560 const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
561 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
562 it != mMousePointersByDisplay.end()) {
563 return it->second->getPosition();
564 }
565 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000566}
567
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900568void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000569 PointerDisplayChange pointerDisplayChange;
570
571 { // acquire lock
572 std::scoped_lock _l(mLock);
573 if (mShowTouchesEnabled == enabled) {
574 return;
575 }
576 mShowTouchesEnabled = enabled;
577 pointerDisplayChange = updatePointerControllersLocked();
578 } // release lock
579
580 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900581}
582
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900583void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
Prabir Pradhan5a51a222024-03-05 03:54:00 +0000584 PointerDisplayChange pointerDisplayChange;
585
586 { // acquire lock
587 std::scoped_lock _l(mLock);
588 if (mStylusPointerIconEnabled == enabled) {
589 return;
590 }
591 mStylusPointerIconEnabled = enabled;
592 pointerDisplayChange = updatePointerControllersLocked();
593 } // release lock
594
595 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900596}
597
Byoungho Jung99326452023-11-03 20:19:17 +0900598bool PointerChoreographer::setPointerIcon(
599 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, int32_t displayId,
600 DeviceId deviceId) {
601 std::scoped_lock _l(mLock);
602 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000603 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900604 return false;
605 }
606 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
607 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000608 LOG(WARNING) << "No input device info found for id " << deviceId
609 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900610 return false;
611 }
612 const uint32_t sources = info->getSources();
Byoungho Jung99326452023-11-03 20:19:17 +0900613
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000614 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) {
615 auto it = mDrawingTabletPointersByDevice.find(deviceId);
616 if (it != mDrawingTabletPointersByDevice.end()) {
617 setIconForController(icon, *it->second);
618 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900619 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000620 }
621 if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) {
622 auto it = mStylusPointersByDevice.find(deviceId);
623 if (it != mStylusPointersByDevice.end()) {
624 setIconForController(icon, *it->second);
625 return true;
626 }
627 }
628 if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
629 auto it = mMousePointersByDisplay.find(displayId);
630 if (it != mMousePointersByDisplay.end()) {
631 setIconForController(icon, *it->second);
632 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900633 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000634 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
635 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900636 return false;
637 }
Byoungho Jung99326452023-11-03 20:19:17 +0900638 }
Prabir Pradhan4c977a42024-03-15 16:47:37 +0000639 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId
640 << ".";
641 return false;
Byoungho Jung99326452023-11-03 20:19:17 +0900642}
643
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000644void PointerChoreographer::setPointerIconVisibility(int32_t displayId, bool visible) {
645 std::scoped_lock lock(mLock);
646 if (visible) {
647 mDisplaysWithPointersHidden.erase(displayId);
648 // We do not unfade the icons here, because we don't know when the last event happened.
649 return;
650 }
651
652 mDisplaysWithPointersHidden.emplace(displayId);
653
654 // Hide any icons that are currently visible on the display.
655 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
656 const auto& [_, controller] = *it;
657 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
658 }
659 for (const auto& [_, controller] : mStylusPointersByDevice) {
660 if (controller->getDisplayId() == displayId) {
661 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
662 }
663 }
664}
665
Prabir Pradhan19767602023-11-03 16:53:31 +0000666PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
667 int32_t displayId) {
668 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
669 [this, displayId]() REQUIRES(mLock) {
670 auto pc = mPolicy.createPointerController(
671 PointerControllerInterface::ControllerType::MOUSE);
672 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
673 pc->setDisplayViewport(*viewport);
674 }
675 return pc;
676 };
677 return ConstructorDelegate(std::move(ctor));
678}
679
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900680PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
681 int32_t displayId) {
682 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
683 [this, displayId]() REQUIRES(mLock) {
684 auto pc = mPolicy.createPointerController(
685 PointerControllerInterface::ControllerType::STYLUS);
686 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
687 pc->setDisplayViewport(*viewport);
688 }
689 return pc;
690 };
691 return ConstructorDelegate(std::move(ctor));
692}
693
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000694} // namespace android