blob: 36e133b375a1d8c6666cb639e71e2f12cd2ecd2f [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 Pradhana955ada2024-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 Pradhan7fe89272024-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 Pradhana955ada2024-03-05 03:54:00 +000053
Prabir Pradhan7fe89272024-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 Pradhana955ada2024-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 Pradhan7fe89272024-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 Pradhana955ada2024-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 Pradhana955ada2024-03-05 03:54:00 +0000100 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101
Prabir Pradhana955ada2024-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 Pradhan7fe89272024-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 Pradhan30ed2c02024-03-05 00:31:36 +0000150 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900151
152 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
153 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
154 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000155 if (canUnfadeOnDisplay(displayId)) {
156 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
157 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900158
159 const auto [x, y] = pc.getPosition();
160 NotifyMotionArgs newArgs(args);
161 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
162 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
163 newArgs.xCursorPosition = x;
164 newArgs.yCursorPosition = y;
165 newArgs.displayId = displayId;
166 return newArgs;
167}
168
Byoungho Jungee6268f2023-10-30 17:27:26 +0900169NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
Prabir Pradhan30ed2c02024-03-05 00:31:36 +0000170 auto [displayId, pc] = ensureMouseControllerLocked(args.displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900171
172 NotifyMotionArgs newArgs(args);
173 newArgs.displayId = displayId;
174 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
175 // This is a movement of the mouse pointer.
176 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
177 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
178 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000179 if (canUnfadeOnDisplay(displayId)) {
180 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
181 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900182
183 const auto [x, y] = pc.getPosition();
184 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
185 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
186 newArgs.xCursorPosition = x;
187 newArgs.yCursorPosition = y;
188 } else {
189 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
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 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
196 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
197 args.pointerCoords[i].getX() + x);
198 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
199 args.pointerCoords[i].getY() + y);
200 }
201 newArgs.xCursorPosition = x;
202 newArgs.yCursorPosition = y;
203 }
204 return newArgs;
205}
206
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000207void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) {
208 if (args.displayId == ADISPLAY_ID_NONE) {
209 return;
210 }
211
212 if (args.getPointerCount() != 1) {
213 LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: "
214 << args.dump();
215 }
216
217 // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist.
218 auto [it, _] = mDrawingTabletPointersByDevice.try_emplace(args.deviceId,
219 getMouseControllerConstructor(
220 args.displayId));
221
222 PointerControllerInterface& pc = *it->second;
223
224 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
225 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
226 pc.setPosition(x, y);
227 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
228 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
229 // immediately by a DOWN event.
230 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
231 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
232 } else if (canUnfadeOnDisplay(args.displayId)) {
233 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
234 }
235}
236
Byoungho Jungda10dd32023-10-06 17:03:45 +0900237/**
238 * When screen is touched, fade the mouse pointer on that display. We only call fade for
239 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
240 * mouse device keeps moving and unfades the cursor.
241 * For touch events, we do not need to populate the cursor position.
242 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900243void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
244 if (args.displayId == ADISPLAY_ID_NONE) {
245 return;
246 }
247
Byoungho Jungda10dd32023-10-06 17:03:45 +0900248 if (const auto it = mMousePointersByDisplay.find(args.displayId);
249 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
250 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
251 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900252
253 if (!mShowTouchesEnabled) {
254 return;
255 }
256
257 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Prabir Pradhan16788792023-11-08 21:07:21 +0000258 auto [it, _] = mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900259
260 PointerControllerInterface& pc = *it->second;
261
262 const PointerCoords* coords = args.pointerCoords.data();
263 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
264 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
265 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
266 BitSet32 idBits;
267 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL) {
268 for (size_t i = 0; i < args.getPointerCount(); i++) {
269 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
270 continue;
271 }
272 uint32_t id = args.pointerProperties[i].id;
273 idToIndex[id] = i;
274 idBits.markBit(id);
275 }
276 }
277 // The PointerController already handles setting spots per-display, so
278 // we do not need to manually manage display changes for touch spots for now.
279 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000280}
281
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900282void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
283 if (args.displayId == ADISPLAY_ID_NONE) {
284 return;
285 }
286
287 if (args.getPointerCount() != 1) {
288 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
289 << args.dump();
290 }
291
292 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
293 auto [it, _] =
294 mStylusPointersByDevice.try_emplace(args.deviceId,
295 getStylusControllerConstructor(args.displayId));
296
297 PointerControllerInterface& pc = *it->second;
298
299 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
300 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
301 pc.setPosition(x, y);
302 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000303 // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed
304 // immediately by a DOWN event.
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900305 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan4b36db92024-01-03 20:56:57 +0000306 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000307 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900308 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
309 }
310}
311
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000312void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
313 mNextListener.notify(args);
314}
315
316void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
317 mNextListener.notify(args);
318}
319
320void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
321 mNextListener.notify(args);
322}
323
324void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900325 processDeviceReset(args);
326
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000327 mNextListener.notify(args);
328}
329
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900330void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
331 std::scoped_lock _l(mLock);
Prabir Pradhan16788792023-11-08 21:07:21 +0000332 mTouchPointersByDevice.erase(args.deviceId);
333 mStylusPointersByDevice.erase(args.deviceId);
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000334 mDrawingTabletPointersByDevice.erase(args.deviceId);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900335}
336
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000337void PointerChoreographer::notifyPointerCaptureChanged(
338 const NotifyPointerCaptureChangedArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900339 if (args.request.enable) {
340 std::scoped_lock _l(mLock);
341 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
342 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
343 }
344 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000345 mNextListener.notify(args);
346}
347
348void PointerChoreographer::dump(std::string& dump) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900349 std::scoped_lock _l(mLock);
350
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000351 dump += "PointerChoreographer:\n";
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900352 dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false");
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900353 dump += StringPrintf("stylus pointer icon enabled: %s\n",
354 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900355
356 dump += INDENT "MousePointerControllers:\n";
357 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
358 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
359 dump += INDENT + std::to_string(displayId) + " : " + pointerControllerDump;
360 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900361 dump += INDENT "TouchPointerControllers:\n";
362 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
363 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
364 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
365 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900366 dump += INDENT "StylusPointerControllers:\n";
367 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
368 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
369 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
370 }
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000371 dump += INDENT "DrawingTabletControllers:\n";
372 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
373 std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT);
374 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
375 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900376 dump += "\n";
377}
378
379const DisplayViewport* PointerChoreographer::findViewportByIdLocked(int32_t displayId) const {
380 for (auto& viewport : mViewports) {
381 if (viewport.displayId == displayId) {
382 return &viewport;
383 }
384 }
385 return nullptr;
386}
387
388int32_t PointerChoreographer::getTargetMouseDisplayLocked(int32_t associatedDisplayId) const {
389 return associatedDisplayId == ADISPLAY_ID_NONE ? mDefaultMouseDisplayId : associatedDisplayId;
390}
391
Prabir Pradhan30ed2c02024-03-05 00:31:36 +0000392std::pair<int32_t, PointerControllerInterface&> PointerChoreographer::ensureMouseControllerLocked(
393 int32_t associatedDisplayId) {
Byoungho Jungee6268f2023-10-30 17:27:26 +0900394 const int32_t displayId = getTargetMouseDisplayLocked(associatedDisplayId);
395
Prabir Pradhan30ed2c02024-03-05 00:31:36 +0000396 auto it = mMousePointersByDisplay.find(displayId);
397 LOG_ALWAYS_FATAL_IF(it == mMousePointersByDisplay.end(),
398 "There is no mouse controller created for display %d", displayId);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900399
400 return {displayId, *it->second};
401}
402
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900403InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000404 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
405 [deviceId](const auto& info) { return info.getId() == deviceId; });
406 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900407}
408
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000409bool PointerChoreographer::canUnfadeOnDisplay(int32_t displayId) {
410 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
411}
412
Prabir Pradhana955ada2024-03-05 03:54:00 +0000413PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900414 std::set<int32_t /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900415 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900416 std::set<DeviceId> stylusDevicesToKeep;
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000417 std::set<DeviceId> drawingTabletDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900418
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000419 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
420 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900421 for (const auto& info : mInputDeviceInfos) {
422 const uint32_t sources = info.getSources();
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000423 if (isMouseOrTouchpad(sources)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000424 const int32_t displayId = getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
425 mouseDisplaysToKeep.insert(displayId);
426 // For mice, show the cursor immediately when the device is first connected or
427 // when it moves to a new display.
428 auto [mousePointerIt, isNewMousePointer] =
429 mMousePointersByDisplay.try_emplace(displayId,
430 getMouseControllerConstructor(displayId));
431 auto [_, isNewMouseDevice] = mMouseDevices.emplace(info.getId());
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000432 if ((isNewMouseDevice || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000433 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
434 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900435 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900436 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
437 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
438 touchDevicesToKeep.insert(info.getId());
439 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900440 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
441 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
442 stylusDevicesToKeep.insert(info.getId());
443 }
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000444 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) &&
445 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
446 drawingTabletDevicesToKeep.insert(info.getId());
447 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900448 }
449
450 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000451 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000452 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900453 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900454 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000455 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900456 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900457 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000458 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900459 });
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000460 std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) {
461 return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end();
462 });
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000463 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(mLock) {
464 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
465 [id](const auto& info) { return info.getId() == id; }) ==
466 mInputDeviceInfos.end();
467 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900468
Prabir Pradhana955ada2024-03-05 03:54:00 +0000469 // Check if we need to notify the policy if there's a change on the pointer display ID.
470 return calculatePointerDisplayChangeToNotify();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900471}
472
Prabir Pradhana955ada2024-03-05 03:54:00 +0000473PointerChoreographer::PointerDisplayChange
474PointerChoreographer::calculatePointerDisplayChangeToNotify() {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900475 int32_t displayIdToNotify = ADISPLAY_ID_NONE;
476 FloatPoint cursorPosition = {0, 0};
477 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
478 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000479 const auto& pointerController = it->second;
480 // Use the displayId from the pointerController, because it accurately reflects whether
481 // the viewport has been added for that display. Otherwise, we would have to check if
482 // the viewport exists separately.
483 displayIdToNotify = pointerController->getDisplayId();
484 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900485 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900486 if (mNotifiedPointerDisplayId == displayIdToNotify) {
Prabir Pradhana955ada2024-03-05 03:54:00 +0000487 return {};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900488 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900489 mNotifiedPointerDisplayId = displayIdToNotify;
Prabir Pradhana955ada2024-03-05 03:54:00 +0000490 return {{displayIdToNotify, cursorPosition}};
Byoungho Jungda10dd32023-10-06 17:03:45 +0900491}
492
493void PointerChoreographer::setDefaultMouseDisplayId(int32_t displayId) {
Prabir Pradhana955ada2024-03-05 03:54:00 +0000494 PointerDisplayChange pointerDisplayChange;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900495
Prabir Pradhana955ada2024-03-05 03:54:00 +0000496 { // acquire lock
497 std::scoped_lock _l(mLock);
498
499 mDefaultMouseDisplayId = displayId;
500 pointerDisplayChange = updatePointerControllersLocked();
501 } // release lock
502
503 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900504}
505
506void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
Prabir Pradhana955ada2024-03-05 03:54:00 +0000507 PointerDisplayChange pointerDisplayChange;
508
509 { // acquire lock
510 std::scoped_lock _l(mLock);
511 for (const auto& viewport : viewports) {
512 const int32_t displayId = viewport.displayId;
513 if (const auto it = mMousePointersByDisplay.find(displayId);
514 it != mMousePointersByDisplay.end()) {
515 it->second->setDisplayViewport(viewport);
516 }
517 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
518 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
519 if (info && info->getAssociatedDisplayId() == displayId) {
520 stylusPointerController->setDisplayViewport(viewport);
521 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900522 }
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000523 for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) {
524 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
525 if (info && info->getAssociatedDisplayId() == displayId) {
526 drawingTabletController->setDisplayViewport(viewport);
527 }
528 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900529 }
Prabir Pradhana955ada2024-03-05 03:54:00 +0000530 mViewports = viewports;
531 pointerDisplayChange = calculatePointerDisplayChangeToNotify();
532 } // release lock
533
534 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900535}
536
537std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
538 int32_t associatedDisplayId) {
539 std::scoped_lock _l(mLock);
540 const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
541 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
542 return *viewport;
543 }
544 return std::nullopt;
545}
546
547FloatPoint PointerChoreographer::getMouseCursorPosition(int32_t displayId) {
548 std::scoped_lock _l(mLock);
549 const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
550 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
551 it != mMousePointersByDisplay.end()) {
552 return it->second->getPosition();
553 }
554 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000555}
556
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900557void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
Prabir Pradhana955ada2024-03-05 03:54:00 +0000558 PointerDisplayChange pointerDisplayChange;
559
560 { // acquire lock
561 std::scoped_lock _l(mLock);
562 if (mShowTouchesEnabled == enabled) {
563 return;
564 }
565 mShowTouchesEnabled = enabled;
566 pointerDisplayChange = updatePointerControllersLocked();
567 } // release lock
568
569 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900570}
571
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900572void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
Prabir Pradhana955ada2024-03-05 03:54:00 +0000573 PointerDisplayChange pointerDisplayChange;
574
575 { // acquire lock
576 std::scoped_lock _l(mLock);
577 if (mStylusPointerIconEnabled == enabled) {
578 return;
579 }
580 mStylusPointerIconEnabled = enabled;
581 pointerDisplayChange = updatePointerControllersLocked();
582 } // release lock
583
584 notifyPointerDisplayChange(pointerDisplayChange, mPolicy);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900585}
586
Byoungho Jung99326452023-11-03 20:19:17 +0900587bool PointerChoreographer::setPointerIcon(
588 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, int32_t displayId,
589 DeviceId deviceId) {
590 std::scoped_lock _l(mLock);
591 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000592 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900593 return false;
594 }
595 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
596 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000597 LOG(WARNING) << "No input device info found for id " << deviceId
598 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900599 return false;
600 }
601 const uint32_t sources = info->getSources();
Byoungho Jung99326452023-11-03 20:19:17 +0900602
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000603 if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) {
604 auto it = mDrawingTabletPointersByDevice.find(deviceId);
605 if (it != mDrawingTabletPointersByDevice.end()) {
606 setIconForController(icon, *it->second);
607 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900608 }
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000609 }
610 if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) {
611 auto it = mStylusPointersByDevice.find(deviceId);
612 if (it != mStylusPointersByDevice.end()) {
613 setIconForController(icon, *it->second);
614 return true;
615 }
616 }
617 if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
618 auto it = mMousePointersByDisplay.find(displayId);
619 if (it != mMousePointersByDisplay.end()) {
620 setIconForController(icon, *it->second);
621 return true;
Byoungho Jung99326452023-11-03 20:19:17 +0900622 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000623 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
624 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900625 return false;
626 }
Byoungho Jung99326452023-11-03 20:19:17 +0900627 }
Prabir Pradhan7fe89272024-03-15 16:47:37 +0000628 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId
629 << ".";
630 return false;
Byoungho Jung99326452023-11-03 20:19:17 +0900631}
632
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000633void PointerChoreographer::setPointerIconVisibility(int32_t displayId, bool visible) {
634 std::scoped_lock lock(mLock);
635 if (visible) {
636 mDisplaysWithPointersHidden.erase(displayId);
637 // We do not unfade the icons here, because we don't know when the last event happened.
638 return;
639 }
640
641 mDisplaysWithPointersHidden.emplace(displayId);
642
643 // Hide any icons that are currently visible on the display.
644 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
645 const auto& [_, controller] = *it;
646 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
647 }
648 for (const auto& [_, controller] : mStylusPointersByDevice) {
649 if (controller->getDisplayId() == displayId) {
650 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
651 }
652 }
653}
654
Prabir Pradhan19767602023-11-03 16:53:31 +0000655PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
656 int32_t displayId) {
657 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
658 [this, displayId]() REQUIRES(mLock) {
659 auto pc = mPolicy.createPointerController(
660 PointerControllerInterface::ControllerType::MOUSE);
661 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
662 pc->setDisplayViewport(*viewport);
663 }
664 return pc;
665 };
666 return ConstructorDelegate(std::move(ctor));
667}
668
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900669PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
670 int32_t displayId) {
671 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
672 [this, displayId]() REQUIRES(mLock) {
673 auto pc = mPolicy.createPointerController(
674 PointerControllerInterface::ControllerType::STYLUS);
675 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
676 pc->setDisplayViewport(*viewport);
677 }
678 return pc;
679 };
680 return ConstructorDelegate(std::move(ctor));
681}
682
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000683} // namespace android