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 | a955ada | 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 | 7fe8927 | 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 | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 53 | |
Prabir Pradhan | 7fe8927 | 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 | a955ada | 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 | 7fe8927 | 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 | a955ada | 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 | a955ada | 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 | a955ada | 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 | 7fe8927 | 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 | 30ed2c0 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 150 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 151 | |
| 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 155 | if (canUnfadeOnDisplay(displayId)) { |
| 156 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 157 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 158 | |
| 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 Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 169 | NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) { |
Prabir Pradhan | 30ed2c0 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 170 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 171 | |
| 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 179 | if (canUnfadeOnDisplay(displayId)) { |
| 180 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 181 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 182 | |
| 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 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 | 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 Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 207 | void 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 237 | /** |
| 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 243 | void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) { |
| 244 | if (args.displayId == ADISPLAY_ID_NONE) { |
| 245 | return; |
| 246 | } |
| 247 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 248 | 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 252 | |
| 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 Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 258 | auto [it, _] = mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 259 | |
| 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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 282 | void 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 Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 303 | // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed |
| 304 | // immediately by a DOWN event. |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 305 | pc.fade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | 4b36db9 | 2024-01-03 20:56:57 +0000 | [diff] [blame] | 306 | pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED); |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 307 | } else if (canUnfadeOnDisplay(args.displayId)) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 308 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 309 | } |
| 310 | } |
| 311 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 312 | void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) { |
| 313 | mNextListener.notify(args); |
| 314 | } |
| 315 | |
| 316 | void PointerChoreographer::notifySensor(const NotifySensorArgs& args) { |
| 317 | mNextListener.notify(args); |
| 318 | } |
| 319 | |
| 320 | void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
| 321 | mNextListener.notify(args); |
| 322 | } |
| 323 | |
| 324 | void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 325 | processDeviceReset(args); |
| 326 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 327 | mNextListener.notify(args); |
| 328 | } |
| 329 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 330 | void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) { |
| 331 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 332 | mTouchPointersByDevice.erase(args.deviceId); |
| 333 | mStylusPointersByDevice.erase(args.deviceId); |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 334 | mDrawingTabletPointersByDevice.erase(args.deviceId); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 337 | void PointerChoreographer::notifyPointerCaptureChanged( |
| 338 | const NotifyPointerCaptureChangedArgs& args) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 339 | 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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 345 | mNextListener.notify(args); |
| 346 | } |
| 347 | |
| 348 | void PointerChoreographer::dump(std::string& dump) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 349 | std::scoped_lock _l(mLock); |
| 350 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 351 | dump += "PointerChoreographer:\n"; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 352 | dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false"); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 353 | dump += StringPrintf("stylus pointer icon enabled: %s\n", |
| 354 | mStylusPointerIconEnabled ? "true" : "false"); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 355 | |
| 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 361 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 366 | 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 Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 371 | 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 376 | dump += "\n"; |
| 377 | } |
| 378 | |
| 379 | const 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 | |
| 388 | int32_t PointerChoreographer::getTargetMouseDisplayLocked(int32_t associatedDisplayId) const { |
| 389 | return associatedDisplayId == ADISPLAY_ID_NONE ? mDefaultMouseDisplayId : associatedDisplayId; |
| 390 | } |
| 391 | |
Prabir Pradhan | 30ed2c0 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 392 | std::pair<int32_t, PointerControllerInterface&> PointerChoreographer::ensureMouseControllerLocked( |
| 393 | int32_t associatedDisplayId) { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 394 | const int32_t displayId = getTargetMouseDisplayLocked(associatedDisplayId); |
| 395 | |
Prabir Pradhan | 30ed2c0 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 396 | 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 Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 399 | |
| 400 | return {displayId, *it->second}; |
| 401 | } |
| 402 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 403 | InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 404 | 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 407 | } |
| 408 | |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 409 | bool PointerChoreographer::canUnfadeOnDisplay(int32_t displayId) { |
| 410 | return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end(); |
| 411 | } |
| 412 | |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 413 | PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 414 | std::set<int32_t /*displayId*/> mouseDisplaysToKeep; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 415 | std::set<DeviceId> touchDevicesToKeep; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 416 | std::set<DeviceId> stylusDevicesToKeep; |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 417 | std::set<DeviceId> drawingTabletDevicesToKeep; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 418 | |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 419 | // Mark the displayIds or deviceIds of PointerControllers currently needed, and create |
| 420 | // new PointerControllers if necessary. |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 421 | for (const auto& info : mInputDeviceInfos) { |
| 422 | const uint32_t sources = info.getSources(); |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 423 | if (isMouseOrTouchpad(sources)) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 424 | 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 Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 432 | if ((isNewMouseDevice || isNewMousePointer) && canUnfadeOnDisplay(displayId)) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 433 | mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 434 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 435 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 436 | if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled && |
| 437 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 438 | touchDevicesToKeep.insert(info.getId()); |
| 439 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 440 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled && |
| 441 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 442 | stylusDevicesToKeep.insert(info.getId()); |
| 443 | } |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 444 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) && |
| 445 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 446 | drawingTabletDevicesToKeep.insert(info.getId()); |
| 447 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Remove PointerControllers no longer needed. |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 451 | std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 452 | return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 453 | }); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 454 | std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 455 | return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end(); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 456 | }); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 457 | std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 458 | return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end(); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 459 | }); |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 460 | std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) { |
| 461 | return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end(); |
| 462 | }); |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 463 | 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 468 | |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 469 | // Check if we need to notify the policy if there's a change on the pointer display ID. |
| 470 | return calculatePointerDisplayChangeToNotify(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 471 | } |
| 472 | |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 473 | PointerChoreographer::PointerDisplayChange |
| 474 | PointerChoreographer::calculatePointerDisplayChangeToNotify() { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 475 | int32_t displayIdToNotify = ADISPLAY_ID_NONE; |
| 476 | FloatPoint cursorPosition = {0, 0}; |
| 477 | if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId); |
| 478 | it != mMousePointersByDisplay.end()) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 479 | 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 Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 485 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 486 | if (mNotifiedPointerDisplayId == displayIdToNotify) { |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 487 | return {}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 488 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 489 | mNotifiedPointerDisplayId = displayIdToNotify; |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 490 | return {{displayIdToNotify, cursorPosition}}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void PointerChoreographer::setDefaultMouseDisplayId(int32_t displayId) { |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 494 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 495 | |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 496 | { // acquire lock |
| 497 | std::scoped_lock _l(mLock); |
| 498 | |
| 499 | mDefaultMouseDisplayId = displayId; |
| 500 | pointerDisplayChange = updatePointerControllersLocked(); |
| 501 | } // release lock |
| 502 | |
| 503 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) { |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 507 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 522 | } |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 523 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 529 | } |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 530 | mViewports = viewports; |
| 531 | pointerDisplayChange = calculatePointerDisplayChangeToNotify(); |
| 532 | } // release lock |
| 533 | |
| 534 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | std::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 | |
| 547 | FloatPoint 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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 557 | void PointerChoreographer::setShowTouchesEnabled(bool enabled) { |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 558 | 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 Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 570 | } |
| 571 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 572 | void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) { |
Prabir Pradhan | a955ada | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 573 | 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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 585 | } |
| 586 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 587 | bool 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 Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 592 | LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 593 | return false; |
| 594 | } |
| 595 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 596 | if (!info) { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 597 | LOG(WARNING) << "No input device info found for id " << deviceId |
| 598 | << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 599 | return false; |
| 600 | } |
| 601 | const uint32_t sources = info->getSources(); |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 602 | |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 603 | 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 Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 608 | } |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 609 | } |
| 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 Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 622 | } else { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 623 | LOG(WARNING) << "No mouse pointer controller found for display " << displayId |
| 624 | << ", device " << deviceId << "."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 625 | return false; |
| 626 | } |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 627 | } |
Prabir Pradhan | 7fe8927 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 628 | LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId |
| 629 | << "."; |
| 630 | return false; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 631 | } |
| 632 | |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 633 | void 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 Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 655 | PointerChoreographer::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 Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 669 | PointerChoreographer::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 Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 683 | } // namespace android |