blob: 3ac4285304be223e36af2b4c39a1a1c1d9f129c2 [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 {
29bool isFromMouse(const NotifyMotionArgs& args) {
30 return isFromSource(args.source, AINPUT_SOURCE_MOUSE) &&
31 args.pointerProperties[0].toolType == ToolType::MOUSE;
32}
33
Byoungho Jungee6268f2023-10-30 17:27:26 +090034bool isFromTouchpad(const NotifyMotionArgs& args) {
35 return isFromSource(args.source, AINPUT_SOURCE_MOUSE) &&
36 args.pointerProperties[0].toolType == ToolType::FINGER;
37}
38
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090039bool isHoverAction(int32_t action) {
40 return action == AMOTION_EVENT_ACTION_HOVER_ENTER ||
41 action == AMOTION_EVENT_ACTION_HOVER_MOVE || action == AMOTION_EVENT_ACTION_HOVER_EXIT;
42}
43
44bool isStylusHoverEvent(const NotifyMotionArgs& args) {
45 return isStylusEvent(args.source, args.pointerProperties) && isHoverAction(args.action);
46}
Byoungho Jungda10dd32023-10-06 17:03:45 +090047} // namespace
48
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000049// --- PointerChoreographer ---
50
51PointerChoreographer::PointerChoreographer(InputListenerInterface& listener,
52 PointerChoreographerPolicyInterface& policy)
Prabir Pradhan16788792023-11-08 21:07:21 +000053 : mTouchControllerConstructor([this]() REQUIRES(mLock) {
54 return mPolicy.createPointerController(
55 PointerControllerInterface::ControllerType::TOUCH);
56 }),
57 mNextListener(listener),
Byoungho Jungda10dd32023-10-06 17:03:45 +090058 mPolicy(policy),
59 mDefaultMouseDisplayId(ADISPLAY_ID_DEFAULT),
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090060 mNotifiedPointerDisplayId(ADISPLAY_ID_NONE),
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090061 mShowTouchesEnabled(false),
62 mStylusPointerIconEnabled(false) {}
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000063
64void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +090065 std::scoped_lock _l(mLock);
66
67 mInputDeviceInfos = args.inputDeviceInfos;
68 updatePointerControllersLocked();
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000069 mNextListener.notify(args);
70}
71
72void PointerChoreographer::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
73 mNextListener.notify(args);
74}
75
76void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) {
77 mNextListener.notify(args);
78}
79
80void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +090081 NotifyMotionArgs newArgs = processMotion(args);
82
83 mNextListener.notify(newArgs);
84}
85
86NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) {
87 std::scoped_lock _l(mLock);
88
89 if (isFromMouse(args)) {
90 return processMouseEventLocked(args);
Byoungho Jungee6268f2023-10-30 17:27:26 +090091 } else if (isFromTouchpad(args)) {
92 return processTouchpadEventLocked(args);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090093 } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) {
94 processStylusHoverEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +090095 } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090096 processTouchscreenAndStylusEventLocked(args);
Byoungho Jungda10dd32023-10-06 17:03:45 +090097 }
98 return args;
99}
100
101NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) {
102 if (args.getPointerCount() != 1) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000103 LOG(FATAL) << "Only mouse events with a single pointer are currently supported: "
104 << args.dump();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900105 }
106
Byoungho Jungee6268f2023-10-30 17:27:26 +0900107 auto [displayId, pc] = getDisplayIdAndMouseControllerLocked(args.displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900108
109 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
110 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
111 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000112 if (canUnfadeOnDisplay(displayId)) {
113 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
114 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900115
116 const auto [x, y] = pc.getPosition();
117 NotifyMotionArgs newArgs(args);
118 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
119 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
120 newArgs.xCursorPosition = x;
121 newArgs.yCursorPosition = y;
122 newArgs.displayId = displayId;
123 return newArgs;
124}
125
Byoungho Jungee6268f2023-10-30 17:27:26 +0900126NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) {
127 auto [displayId, pc] = getDisplayIdAndMouseControllerLocked(args.displayId);
128
129 NotifyMotionArgs newArgs(args);
130 newArgs.displayId = displayId;
131 if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) {
132 // This is a movement of the mouse pointer.
133 const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
134 const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
135 pc.move(deltaX, deltaY);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000136 if (canUnfadeOnDisplay(displayId)) {
137 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
138 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900139
140 const auto [x, y] = pc.getPosition();
141 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
142 newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
143 newArgs.xCursorPosition = x;
144 newArgs.yCursorPosition = y;
145 } else {
146 // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer.
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000147 if (canUnfadeOnDisplay(displayId)) {
148 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
149 }
Byoungho Jungee6268f2023-10-30 17:27:26 +0900150
151 const auto [x, y] = pc.getPosition();
152 for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) {
153 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X,
154 args.pointerCoords[i].getX() + x);
155 newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y,
156 args.pointerCoords[i].getY() + y);
157 }
158 newArgs.xCursorPosition = x;
159 newArgs.yCursorPosition = y;
160 }
161 return newArgs;
162}
163
Byoungho Jungda10dd32023-10-06 17:03:45 +0900164/**
165 * When screen is touched, fade the mouse pointer on that display. We only call fade for
166 * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the
167 * mouse device keeps moving and unfades the cursor.
168 * For touch events, we do not need to populate the cursor position.
169 */
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900170void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) {
171 if (args.displayId == ADISPLAY_ID_NONE) {
172 return;
173 }
174
Byoungho Jungda10dd32023-10-06 17:03:45 +0900175 if (const auto it = mMousePointersByDisplay.find(args.displayId);
176 it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) {
177 it->second->fade(PointerControllerInterface::Transition::GRADUAL);
178 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900179
180 if (!mShowTouchesEnabled) {
181 return;
182 }
183
184 // Get the touch pointer controller for the device, or create one if it doesn't exist.
Prabir Pradhan16788792023-11-08 21:07:21 +0000185 auto [it, _] = mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900186
187 PointerControllerInterface& pc = *it->second;
188
189 const PointerCoords* coords = args.pointerCoords.data();
190 const int32_t maskedAction = MotionEvent::getActionMasked(args.action);
191 const uint8_t actionIndex = MotionEvent::getActionIndex(args.action);
192 std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex;
193 BitSet32 idBits;
194 if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL) {
195 for (size_t i = 0; i < args.getPointerCount(); i++) {
196 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) {
197 continue;
198 }
199 uint32_t id = args.pointerProperties[i].id;
200 idToIndex[id] = i;
201 idBits.markBit(id);
202 }
203 }
204 // The PointerController already handles setting spots per-display, so
205 // we do not need to manually manage display changes for touch spots for now.
206 pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000207}
208
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900209void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) {
210 if (args.displayId == ADISPLAY_ID_NONE) {
211 return;
212 }
213
214 if (args.getPointerCount() != 1) {
215 LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: "
216 << args.dump();
217 }
218
219 // Get the stylus pointer controller for the device, or create one if it doesn't exist.
220 auto [it, _] =
221 mStylusPointersByDevice.try_emplace(args.deviceId,
222 getStylusControllerConstructor(args.displayId));
223
224 PointerControllerInterface& pc = *it->second;
225
226 const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
227 const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
228 pc.setPosition(x, y);
229 if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
230 pc.fade(PointerControllerInterface::Transition::IMMEDIATE);
Prabir Pradhan4b36db92024-01-03 20:56:57 +0000231 pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED);
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000232 } else if (canUnfadeOnDisplay(args.displayId)) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900233 pc.unfade(PointerControllerInterface::Transition::IMMEDIATE);
234 }
235}
236
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000237void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) {
238 mNextListener.notify(args);
239}
240
241void PointerChoreographer::notifySensor(const NotifySensorArgs& args) {
242 mNextListener.notify(args);
243}
244
245void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) {
246 mNextListener.notify(args);
247}
248
249void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900250 processDeviceReset(args);
251
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000252 mNextListener.notify(args);
253}
254
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900255void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) {
256 std::scoped_lock _l(mLock);
Prabir Pradhan16788792023-11-08 21:07:21 +0000257 mTouchPointersByDevice.erase(args.deviceId);
258 mStylusPointersByDevice.erase(args.deviceId);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900259}
260
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000261void PointerChoreographer::notifyPointerCaptureChanged(
262 const NotifyPointerCaptureChangedArgs& args) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900263 if (args.request.enable) {
264 std::scoped_lock _l(mLock);
265 for (const auto& [_, mousePointerController] : mMousePointersByDisplay) {
266 mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
267 }
268 }
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000269 mNextListener.notify(args);
270}
271
272void PointerChoreographer::dump(std::string& dump) {
Byoungho Jungda10dd32023-10-06 17:03:45 +0900273 std::scoped_lock _l(mLock);
274
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000275 dump += "PointerChoreographer:\n";
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900276 dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false");
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900277 dump += StringPrintf("stylus pointer icon enabled: %s\n",
278 mStylusPointerIconEnabled ? "true" : "false");
Byoungho Jungda10dd32023-10-06 17:03:45 +0900279
280 dump += INDENT "MousePointerControllers:\n";
281 for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) {
282 std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT);
283 dump += INDENT + std::to_string(displayId) + " : " + pointerControllerDump;
284 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900285 dump += INDENT "TouchPointerControllers:\n";
286 for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) {
287 std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT);
288 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
289 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900290 dump += INDENT "StylusPointerControllers:\n";
291 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
292 std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT);
293 dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump;
294 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900295 dump += "\n";
296}
297
298const DisplayViewport* PointerChoreographer::findViewportByIdLocked(int32_t displayId) const {
299 for (auto& viewport : mViewports) {
300 if (viewport.displayId == displayId) {
301 return &viewport;
302 }
303 }
304 return nullptr;
305}
306
307int32_t PointerChoreographer::getTargetMouseDisplayLocked(int32_t associatedDisplayId) const {
308 return associatedDisplayId == ADISPLAY_ID_NONE ? mDefaultMouseDisplayId : associatedDisplayId;
309}
310
Byoungho Jungee6268f2023-10-30 17:27:26 +0900311std::pair<int32_t, PointerControllerInterface&>
312PointerChoreographer::getDisplayIdAndMouseControllerLocked(int32_t associatedDisplayId) {
313 const int32_t displayId = getTargetMouseDisplayLocked(associatedDisplayId);
314
315 // Get the mouse pointer controller for the display, or create one if it doesn't exist.
316 auto [it, emplaced] =
317 mMousePointersByDisplay.try_emplace(displayId,
318 getMouseControllerConstructor(displayId));
319 if (emplaced) {
320 notifyPointerDisplayIdChangedLocked();
321 }
322
323 return {displayId, *it->second};
324}
325
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900326InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000327 auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
328 [deviceId](const auto& info) { return info.getId() == deviceId; });
329 return it != mInputDeviceInfos.end() ? &(*it) : nullptr;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900330}
331
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000332bool PointerChoreographer::canUnfadeOnDisplay(int32_t displayId) {
333 return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end();
334}
335
Byoungho Jungda10dd32023-10-06 17:03:45 +0900336void PointerChoreographer::updatePointerControllersLocked() {
337 std::set<int32_t /*displayId*/> mouseDisplaysToKeep;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900338 std::set<DeviceId> touchDevicesToKeep;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900339 std::set<DeviceId> stylusDevicesToKeep;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900340
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000341 // Mark the displayIds or deviceIds of PointerControllers currently needed, and create
342 // new PointerControllers if necessary.
Byoungho Jungda10dd32023-10-06 17:03:45 +0900343 for (const auto& info : mInputDeviceInfos) {
344 const uint32_t sources = info.getSources();
345 if (isFromSource(sources, AINPUT_SOURCE_MOUSE) ||
346 isFromSource(sources, AINPUT_SOURCE_MOUSE_RELATIVE)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000347 const int32_t displayId = getTargetMouseDisplayLocked(info.getAssociatedDisplayId());
348 mouseDisplaysToKeep.insert(displayId);
349 // For mice, show the cursor immediately when the device is first connected or
350 // when it moves to a new display.
351 auto [mousePointerIt, isNewMousePointer] =
352 mMousePointersByDisplay.try_emplace(displayId,
353 getMouseControllerConstructor(displayId));
354 auto [_, isNewMouseDevice] = mMouseDevices.emplace(info.getId());
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000355 if ((isNewMouseDevice || isNewMousePointer) && canUnfadeOnDisplay(displayId)) {
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000356 mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE);
357 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900358 }
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900359 if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled &&
360 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
361 touchDevicesToKeep.insert(info.getId());
362 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900363 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled &&
364 info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) {
365 stylusDevicesToKeep.insert(info.getId());
366 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900367 }
368
369 // Remove PointerControllers no longer needed.
Prabir Pradhan19767602023-11-03 16:53:31 +0000370 std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000371 return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900372 });
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900373 std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000374 return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end();
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900375 });
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900376 std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) {
Prabir Pradhan16788792023-11-08 21:07:21 +0000377 return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end();
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900378 });
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000379 std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(mLock) {
380 return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(),
381 [id](const auto& info) { return info.getId() == id; }) ==
382 mInputDeviceInfos.end();
383 });
Byoungho Jungda10dd32023-10-06 17:03:45 +0900384
385 // Notify the policy if there's a change on the pointer display ID.
386 notifyPointerDisplayIdChangedLocked();
387}
388
389void PointerChoreographer::notifyPointerDisplayIdChangedLocked() {
390 int32_t displayIdToNotify = ADISPLAY_ID_NONE;
391 FloatPoint cursorPosition = {0, 0};
392 if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId);
393 it != mMousePointersByDisplay.end()) {
Prabir Pradhan19767602023-11-03 16:53:31 +0000394 const auto& pointerController = it->second;
395 // Use the displayId from the pointerController, because it accurately reflects whether
396 // the viewport has been added for that display. Otherwise, we would have to check if
397 // the viewport exists separately.
398 displayIdToNotify = pointerController->getDisplayId();
399 cursorPosition = pointerController->getPosition();
Byoungho Jungda10dd32023-10-06 17:03:45 +0900400 }
401
402 if (mNotifiedPointerDisplayId == displayIdToNotify) {
403 return;
404 }
405 mPolicy.notifyPointerDisplayIdChanged(displayIdToNotify, cursorPosition);
406 mNotifiedPointerDisplayId = displayIdToNotify;
407}
408
409void PointerChoreographer::setDefaultMouseDisplayId(int32_t displayId) {
410 std::scoped_lock _l(mLock);
411
412 mDefaultMouseDisplayId = displayId;
413 updatePointerControllersLocked();
414}
415
416void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
417 std::scoped_lock _l(mLock);
418 for (const auto& viewport : viewports) {
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900419 const int32_t displayId = viewport.displayId;
420 if (const auto it = mMousePointersByDisplay.find(displayId);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900421 it != mMousePointersByDisplay.end()) {
422 it->second->setDisplayViewport(viewport);
423 }
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900424 for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) {
425 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
426 if (info && info->getAssociatedDisplayId() == displayId) {
427 stylusPointerController->setDisplayViewport(viewport);
428 }
429 }
Byoungho Jungda10dd32023-10-06 17:03:45 +0900430 }
431 mViewports = viewports;
432 notifyPointerDisplayIdChangedLocked();
433}
434
435std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice(
436 int32_t associatedDisplayId) {
437 std::scoped_lock _l(mLock);
438 const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId);
439 if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) {
440 return *viewport;
441 }
442 return std::nullopt;
443}
444
445FloatPoint PointerChoreographer::getMouseCursorPosition(int32_t displayId) {
446 std::scoped_lock _l(mLock);
447 const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(displayId);
448 if (auto it = mMousePointersByDisplay.find(resolvedDisplayId);
449 it != mMousePointersByDisplay.end()) {
450 return it->second->getPosition();
451 }
452 return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000453}
454
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900455void PointerChoreographer::setShowTouchesEnabled(bool enabled) {
456 std::scoped_lock _l(mLock);
457 if (mShowTouchesEnabled == enabled) {
458 return;
459 }
460 mShowTouchesEnabled = enabled;
461 updatePointerControllersLocked();
462}
463
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900464void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) {
465 std::scoped_lock _l(mLock);
466 if (mStylusPointerIconEnabled == enabled) {
467 return;
468 }
469 mStylusPointerIconEnabled = enabled;
470 updatePointerControllersLocked();
471}
472
Byoungho Jung99326452023-11-03 20:19:17 +0900473bool PointerChoreographer::setPointerIcon(
474 std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, int32_t displayId,
475 DeviceId deviceId) {
476 std::scoped_lock _l(mLock);
477 if (deviceId < 0) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000478 LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900479 return false;
480 }
481 const InputDeviceInfo* info = findInputDeviceLocked(deviceId);
482 if (!info) {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000483 LOG(WARNING) << "No input device info found for id " << deviceId
484 << ". Cannot set pointer icon.";
Byoungho Jung99326452023-11-03 20:19:17 +0900485 return false;
486 }
487 const uint32_t sources = info->getSources();
488 const auto stylusPointerIt = mStylusPointersByDevice.find(deviceId);
489
490 if (isFromSource(sources, AINPUT_SOURCE_STYLUS) &&
491 stylusPointerIt != mStylusPointersByDevice.end()) {
492 if (std::holds_alternative<std::unique_ptr<SpriteIcon>>(icon)) {
493 if (std::get<std::unique_ptr<SpriteIcon>>(icon) == nullptr) {
494 LOG(FATAL) << "SpriteIcon should not be null";
495 }
496 stylusPointerIt->second->setCustomPointerIcon(
497 *std::get<std::unique_ptr<SpriteIcon>>(icon));
498 } else {
499 stylusPointerIt->second->updatePointerIcon(std::get<PointerIconStyle>(icon));
500 }
501 } else if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) {
502 if (const auto mousePointerIt = mMousePointersByDisplay.find(displayId);
503 mousePointerIt != mMousePointersByDisplay.end()) {
504 if (std::holds_alternative<std::unique_ptr<SpriteIcon>>(icon)) {
505 if (std::get<std::unique_ptr<SpriteIcon>>(icon) == nullptr) {
506 LOG(FATAL) << "SpriteIcon should not be null";
507 }
508 mousePointerIt->second->setCustomPointerIcon(
509 *std::get<std::unique_ptr<SpriteIcon>>(icon));
510 } else {
511 mousePointerIt->second->updatePointerIcon(std::get<PointerIconStyle>(icon));
512 }
513 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000514 LOG(WARNING) << "No mouse pointer controller found for display " << displayId
515 << ", device " << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900516 return false;
517 }
518 } else {
Prabir Pradhan521f4fc2023-12-04 19:09:59 +0000519 LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device "
520 << deviceId << ".";
Byoungho Jung99326452023-11-03 20:19:17 +0900521 return false;
522 }
523 return true;
524}
525
Prabir Pradhan502ddbd2024-01-19 02:22:38 +0000526void PointerChoreographer::setPointerIconVisibility(int32_t displayId, bool visible) {
527 std::scoped_lock lock(mLock);
528 if (visible) {
529 mDisplaysWithPointersHidden.erase(displayId);
530 // We do not unfade the icons here, because we don't know when the last event happened.
531 return;
532 }
533
534 mDisplaysWithPointersHidden.emplace(displayId);
535
536 // Hide any icons that are currently visible on the display.
537 if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) {
538 const auto& [_, controller] = *it;
539 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
540 }
541 for (const auto& [_, controller] : mStylusPointersByDevice) {
542 if (controller->getDisplayId() == displayId) {
543 controller->fade(PointerControllerInterface::Transition::IMMEDIATE);
544 }
545 }
546}
547
Prabir Pradhan19767602023-11-03 16:53:31 +0000548PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor(
549 int32_t displayId) {
550 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
551 [this, displayId]() REQUIRES(mLock) {
552 auto pc = mPolicy.createPointerController(
553 PointerControllerInterface::ControllerType::MOUSE);
554 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
555 pc->setDisplayViewport(*viewport);
556 }
557 return pc;
558 };
559 return ConstructorDelegate(std::move(ctor));
560}
561
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900562PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor(
563 int32_t displayId) {
564 std::function<std::shared_ptr<PointerControllerInterface>()> ctor =
565 [this, displayId]() REQUIRES(mLock) {
566 auto pc = mPolicy.createPointerController(
567 PointerControllerInterface::ControllerType::STYLUS);
568 if (const auto viewport = findViewportByIdLocked(displayId); viewport) {
569 pc->setDisplayViewport(*viewport);
570 }
571 return pc;
572 };
573 return ConstructorDelegate(std::move(ctor));
574}
575
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000576} // namespace android