Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 1 | /* |
| 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <input/PrintTools.h> |
| 21 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 22 | #include "PointerChoreographer.h" |
| 23 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 24 | #define INDENT " " |
| 25 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 28 | namespace { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 29 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 30 | bool isFromMouse(const NotifyMotionArgs& args) { |
| 31 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE) && |
| 32 | args.pointerProperties[0].toolType == ToolType::MOUSE; |
| 33 | } |
| 34 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 35 | bool isFromTouchpad(const NotifyMotionArgs& args) { |
| 36 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE) && |
| 37 | args.pointerProperties[0].toolType == ToolType::FINGER; |
| 38 | } |
| 39 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 40 | bool isFromDrawingTablet(const NotifyMotionArgs& args) { |
| 41 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS) && |
| 42 | isStylusToolType(args.pointerProperties[0].toolType); |
| 43 | } |
| 44 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 45 | bool 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 | |
| 50 | bool isStylusHoverEvent(const NotifyMotionArgs& args) { |
| 51 | return isStylusEvent(args.source, args.pointerProperties) && isHoverAction(args.action); |
| 52 | } |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 53 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 54 | bool 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 Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 61 | inline 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 Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 70 | void 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 82 | } // namespace |
| 83 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 84 | // --- PointerChoreographer --- |
| 85 | |
| 86 | PointerChoreographer::PointerChoreographer(InputListenerInterface& listener, |
| 87 | PointerChoreographerPolicyInterface& policy) |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 88 | : mTouchControllerConstructor([this]() { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 89 | return mPolicy.createPointerController( |
| 90 | PointerControllerInterface::ControllerType::TOUCH); |
| 91 | }), |
| 92 | mNextListener(listener), |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 93 | mPolicy(policy), |
| 94 | mDefaultMouseDisplayId(ADISPLAY_ID_DEFAULT), |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 95 | mNotifiedPointerDisplayId(ADISPLAY_ID_NONE), |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 96 | mShowTouchesEnabled(false), |
| 97 | mStylusPointerIconEnabled(false) {} |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 98 | |
| 99 | void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 100 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 101 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 102 | { // 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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 110 | mNextListener.notify(args); |
| 111 | } |
| 112 | |
| 113 | void PointerChoreographer::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
| 114 | mNextListener.notify(args); |
| 115 | } |
| 116 | |
| 117 | void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) { |
| 118 | mNextListener.notify(args); |
| 119 | } |
| 120 | |
| 121 | void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 122 | NotifyMotionArgs newArgs = processMotion(args); |
| 123 | |
| 124 | mNextListener.notify(newArgs); |
| 125 | } |
| 126 | |
| 127 | NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) { |
| 128 | std::scoped_lock _l(mLock); |
| 129 | |
| 130 | if (isFromMouse(args)) { |
| 131 | return processMouseEventLocked(args); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 132 | } else if (isFromTouchpad(args)) { |
| 133 | return processTouchpadEventLocked(args); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 134 | } else if (isFromDrawingTablet(args)) { |
| 135 | processDrawingTabletEventLocked(args); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 136 | } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) { |
| 137 | processStylusHoverEventLocked(args); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 138 | } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 139 | processTouchscreenAndStylusEventLocked(args); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 140 | } |
| 141 | return args; |
| 142 | } |
| 143 | |
| 144 | NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) { |
| 145 | if (args.getPointerCount() != 1) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 146 | LOG(FATAL) << "Only mouse events with a single pointer are currently supported: " |
| 147 | << args.dump(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 150 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 151 | NotifyMotionArgs newArgs(args); |
| 152 | newArgs.displayId = displayId; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 153 | |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 154 | 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 174 | if (canUnfadeOnDisplay(displayId)) { |
| 175 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 176 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 177 | return newArgs; |
| 178 | } |
| 179 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 180 | NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) { |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 181 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 182 | |
| 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 190 | if (canUnfadeOnDisplay(displayId)) { |
| 191 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 192 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 193 | |
| 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 201 | if (canUnfadeOnDisplay(displayId)) { |
| 202 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 203 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 204 | |
| 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 Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 218 | void 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 248 | /** |
| 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 254 | void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) { |
| 255 | if (args.displayId == ADISPLAY_ID_NONE) { |
| 256 | return; |
| 257 | } |
| 258 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 259 | 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 263 | |
| 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 Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 269 | auto [it, _] = mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 270 | |
| 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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 293 | void 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 Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 314 | // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed |
| 315 | // immediately by a DOWN event. |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 316 | pc.fade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | 4b36db9 | 2024-01-03 20:56:57 +0000 | [diff] [blame] | 317 | pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED); |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 318 | } else if (canUnfadeOnDisplay(args.displayId)) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 319 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 320 | } |
| 321 | } |
| 322 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 323 | void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) { |
| 324 | mNextListener.notify(args); |
| 325 | } |
| 326 | |
| 327 | void PointerChoreographer::notifySensor(const NotifySensorArgs& args) { |
| 328 | mNextListener.notify(args); |
| 329 | } |
| 330 | |
| 331 | void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
| 332 | mNextListener.notify(args); |
| 333 | } |
| 334 | |
| 335 | void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 336 | processDeviceReset(args); |
| 337 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 338 | mNextListener.notify(args); |
| 339 | } |
| 340 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 341 | void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) { |
| 342 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 343 | mTouchPointersByDevice.erase(args.deviceId); |
| 344 | mStylusPointersByDevice.erase(args.deviceId); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 345 | mDrawingTabletPointersByDevice.erase(args.deviceId); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 346 | } |
| 347 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 348 | void PointerChoreographer::notifyPointerCaptureChanged( |
| 349 | const NotifyPointerCaptureChangedArgs& args) { |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 350 | if (args.request.isEnable()) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 351 | std::scoped_lock _l(mLock); |
| 352 | for (const auto& [_, mousePointerController] : mMousePointersByDisplay) { |
| 353 | mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 354 | } |
| 355 | } |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 356 | mNextListener.notify(args); |
| 357 | } |
| 358 | |
| 359 | void PointerChoreographer::dump(std::string& dump) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 360 | std::scoped_lock _l(mLock); |
| 361 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 362 | dump += "PointerChoreographer:\n"; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 363 | dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false"); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 364 | dump += StringPrintf("stylus pointer icon enabled: %s\n", |
| 365 | mStylusPointerIconEnabled ? "true" : "false"); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 366 | |
| 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 372 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 377 | 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 Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 382 | 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 387 | dump += "\n"; |
| 388 | } |
| 389 | |
| 390 | const 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 | |
| 399 | int32_t PointerChoreographer::getTargetMouseDisplayLocked(int32_t associatedDisplayId) const { |
| 400 | return associatedDisplayId == ADISPLAY_ID_NONE ? mDefaultMouseDisplayId : associatedDisplayId; |
| 401 | } |
| 402 | |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 403 | std::pair<int32_t, PointerControllerInterface&> PointerChoreographer::ensureMouseControllerLocked( |
| 404 | int32_t associatedDisplayId) { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 405 | const int32_t displayId = getTargetMouseDisplayLocked(associatedDisplayId); |
| 406 | |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 407 | 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 Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 410 | |
| 411 | return {displayId, *it->second}; |
| 412 | } |
| 413 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 414 | InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 415 | 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 418 | } |
| 419 | |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 420 | bool PointerChoreographer::canUnfadeOnDisplay(int32_t displayId) { |
| 421 | return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end(); |
| 422 | } |
| 423 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 424 | PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 425 | std::set<int32_t /*displayId*/> mouseDisplaysToKeep; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 426 | std::set<DeviceId> touchDevicesToKeep; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 427 | std::set<DeviceId> stylusDevicesToKeep; |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 428 | std::set<DeviceId> drawingTabletDevicesToKeep; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 429 | |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 430 | // Mark the displayIds or deviceIds of PointerControllers currently needed, and create |
| 431 | // new PointerControllers if necessary. |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 432 | for (const auto& info : mInputDeviceInfos) { |
| 433 | const uint32_t sources = info.getSources(); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 434 | if (isMouseOrTouchpad(sources)) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 435 | 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 443 | if ((isNewMouseDevice || isNewMousePointer) && canUnfadeOnDisplay(displayId)) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 444 | mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 445 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 446 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 447 | if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled && |
| 448 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 449 | touchDevicesToKeep.insert(info.getId()); |
| 450 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 451 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled && |
| 452 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 453 | stylusDevicesToKeep.insert(info.getId()); |
| 454 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 455 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) && |
| 456 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 457 | drawingTabletDevicesToKeep.insert(info.getId()); |
| 458 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Remove PointerControllers no longer needed. |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 462 | std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 463 | return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 464 | }); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 465 | std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 466 | return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end(); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 467 | }); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 468 | std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 469 | return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end(); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 470 | }); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 471 | std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) { |
| 472 | return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end(); |
| 473 | }); |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 474 | 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 479 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 480 | // Check if we need to notify the policy if there's a change on the pointer display ID. |
| 481 | return calculatePointerDisplayChangeToNotify(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 482 | } |
| 483 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 484 | PointerChoreographer::PointerDisplayChange |
| 485 | PointerChoreographer::calculatePointerDisplayChangeToNotify() { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 486 | int32_t displayIdToNotify = ADISPLAY_ID_NONE; |
| 487 | FloatPoint cursorPosition = {0, 0}; |
| 488 | if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId); |
| 489 | it != mMousePointersByDisplay.end()) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 490 | 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 496 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 497 | if (mNotifiedPointerDisplayId == displayIdToNotify) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 498 | return {}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 499 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 500 | mNotifiedPointerDisplayId = displayIdToNotify; |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 501 | return {{displayIdToNotify, cursorPosition}}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void PointerChoreographer::setDefaultMouseDisplayId(int32_t displayId) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 505 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 506 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 507 | { // acquire lock |
| 508 | std::scoped_lock _l(mLock); |
| 509 | |
| 510 | mDefaultMouseDisplayId = displayId; |
| 511 | pointerDisplayChange = updatePointerControllersLocked(); |
| 512 | } // release lock |
| 513 | |
| 514 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 518 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 533 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 534 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 540 | } |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 541 | mViewports = viewports; |
| 542 | pointerDisplayChange = calculatePointerDisplayChangeToNotify(); |
| 543 | } // release lock |
| 544 | |
| 545 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | std::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 | |
| 558 | FloatPoint 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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 568 | void PointerChoreographer::setShowTouchesEnabled(bool enabled) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 569 | 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 581 | } |
| 582 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 583 | void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 584 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 596 | } |
| 597 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 598 | bool 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 Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 603 | LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 607 | if (!info) { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 608 | LOG(WARNING) << "No input device info found for id " << deviceId |
| 609 | << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 610 | return false; |
| 611 | } |
| 612 | const uint32_t sources = info->getSources(); |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 613 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 614 | 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 Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 619 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 620 | } |
| 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 Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 633 | } else { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 634 | LOG(WARNING) << "No mouse pointer controller found for display " << displayId |
| 635 | << ", device " << deviceId << "."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 636 | return false; |
| 637 | } |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 638 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame^] | 639 | LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId |
| 640 | << "."; |
| 641 | return false; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 642 | } |
| 643 | |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 644 | void 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 Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 666 | PointerChoreographer::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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 680 | PointerChoreographer::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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 694 | } // namespace android |