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> |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 20 | #include <com_android_input_flags.h> |
| 21 | #if defined(__ANDROID__) |
| 22 | #include <gui/SurfaceComposerClient.h> |
| 23 | #endif |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 24 | #include <input/Keyboard.h> |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 25 | #include <input/PrintTools.h> |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 26 | #include <unordered_set> |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 27 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 28 | #include "PointerChoreographer.h" |
| 29 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 30 | #define INDENT " " |
| 31 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 34 | namespace { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 35 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 36 | bool isFromMouse(const NotifyMotionArgs& args) { |
| 37 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE) && |
| 38 | args.pointerProperties[0].toolType == ToolType::MOUSE; |
| 39 | } |
| 40 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 41 | bool isFromTouchpad(const NotifyMotionArgs& args) { |
| 42 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE) && |
| 43 | args.pointerProperties[0].toolType == ToolType::FINGER; |
| 44 | } |
| 45 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 46 | bool isFromDrawingTablet(const NotifyMotionArgs& args) { |
| 47 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS) && |
| 48 | isStylusToolType(args.pointerProperties[0].toolType); |
| 49 | } |
| 50 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 51 | bool isHoverAction(int32_t action) { |
| 52 | return action == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 53 | action == AMOTION_EVENT_ACTION_HOVER_MOVE || action == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 54 | } |
| 55 | |
| 56 | bool isStylusHoverEvent(const NotifyMotionArgs& args) { |
| 57 | return isStylusEvent(args.source, args.pointerProperties) && isHoverAction(args.action); |
| 58 | } |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 59 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 60 | bool isMouseOrTouchpad(uint32_t sources) { |
| 61 | // Check if this is a mouse or touchpad, but not a drawing tablet. |
| 62 | return isFromSource(sources, AINPUT_SOURCE_MOUSE_RELATIVE) || |
| 63 | (isFromSource(sources, AINPUT_SOURCE_MOUSE) && |
| 64 | !isFromSource(sources, AINPUT_SOURCE_STYLUS)); |
| 65 | } |
| 66 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 67 | inline void notifyPointerDisplayChange( |
| 68 | std::optional<std::tuple<ui::LogicalDisplayId, FloatPoint>> change, |
| 69 | PointerChoreographerPolicyInterface& policy) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 70 | if (!change) { |
| 71 | return; |
| 72 | } |
| 73 | const auto& [displayId, cursorPosition] = *change; |
| 74 | policy.notifyPointerDisplayIdChanged(displayId, cursorPosition); |
| 75 | } |
| 76 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 77 | void setIconForController(const std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle>& icon, |
| 78 | PointerControllerInterface& controller) { |
| 79 | if (std::holds_alternative<std::unique_ptr<SpriteIcon>>(icon)) { |
| 80 | if (std::get<std::unique_ptr<SpriteIcon>>(icon) == nullptr) { |
| 81 | LOG(FATAL) << "SpriteIcon should not be null"; |
| 82 | } |
| 83 | controller.setCustomPointerIcon(*std::get<std::unique_ptr<SpriteIcon>>(icon)); |
| 84 | } else { |
| 85 | controller.updatePointerIcon(std::get<PointerIconStyle>(icon)); |
| 86 | } |
| 87 | } |
| 88 | |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 89 | // filters and returns a set of privacy sensitive displays that are currently visible. |
| 90 | std::unordered_set<ui::LogicalDisplayId> getPrivacySensitiveDisplaysFromWindowInfos( |
| 91 | const std::vector<gui::WindowInfo>& windowInfos) { |
| 92 | std::unordered_set<ui::LogicalDisplayId> privacySensitiveDisplays; |
| 93 | for (const auto& windowInfo : windowInfos) { |
| 94 | if (!windowInfo.inputConfig.test(gui::WindowInfo::InputConfig::NOT_VISIBLE) && |
| 95 | windowInfo.inputConfig.test(gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY)) { |
| 96 | privacySensitiveDisplays.insert(windowInfo.displayId); |
| 97 | } |
| 98 | } |
| 99 | return privacySensitiveDisplays; |
| 100 | } |
| 101 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 102 | } // namespace |
| 103 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 104 | // --- PointerChoreographer --- |
| 105 | |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 106 | PointerChoreographer::PointerChoreographer(InputListenerInterface& inputListener, |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 107 | PointerChoreographerPolicyInterface& policy) |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 108 | : PointerChoreographer( |
| 109 | inputListener, policy, |
| 110 | [](const sp<android::gui::WindowInfosListener>& listener) { |
| 111 | auto initialInfo = std::make_pair(std::vector<android::gui::WindowInfo>{}, |
| 112 | std::vector<android::gui::DisplayInfo>{}); |
| 113 | #if defined(__ANDROID__) |
| 114 | SurfaceComposerClient::getDefault()->addWindowInfosListener(listener, |
| 115 | &initialInfo); |
| 116 | #endif |
| 117 | return initialInfo.first; |
| 118 | }, |
| 119 | [](const sp<android::gui::WindowInfosListener>& listener) { |
| 120 | #if defined(__ANDROID__) |
| 121 | SurfaceComposerClient::getDefault()->removeWindowInfosListener(listener); |
| 122 | #endif |
| 123 | }) { |
| 124 | } |
| 125 | |
| 126 | PointerChoreographer::PointerChoreographer( |
| 127 | android::InputListenerInterface& listener, |
| 128 | android::PointerChoreographerPolicyInterface& policy, |
| 129 | const android::PointerChoreographer::WindowListenerRegisterConsumer& registerListener, |
| 130 | const android::PointerChoreographer::WindowListenerUnregisterConsumer& unregisterListener) |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 131 | : mTouchControllerConstructor([this]() { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 132 | return mPolicy.createPointerController( |
| 133 | PointerControllerInterface::ControllerType::TOUCH); |
| 134 | }), |
| 135 | mNextListener(listener), |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 136 | mPolicy(policy), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 137 | mDefaultMouseDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 138 | mNotifiedPointerDisplayId(ui::LogicalDisplayId::INVALID), |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 139 | mShowTouchesEnabled(false), |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 140 | mStylusPointerIconEnabled(false), |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 141 | mCurrentFocusedDisplay(ui::LogicalDisplayId::DEFAULT), |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 142 | mIsWindowInfoListenerRegistered(false), |
| 143 | mWindowInfoListener(sp<PointerChoreographerDisplayInfoListener>::make(this)), |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 144 | mRegisterListener(registerListener), |
| 145 | mUnregisterListener(unregisterListener) {} |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 146 | |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 147 | PointerChoreographer::~PointerChoreographer() { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 148 | if (mIsWindowInfoListenerRegistered) { |
| 149 | mUnregisterListener(mWindowInfoListener); |
| 150 | mIsWindowInfoListenerRegistered = false; |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 151 | } |
| 152 | mWindowInfoListener->onPointerChoreographerDestroyed(); |
| 153 | } |
| 154 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 155 | void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 156 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 157 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 158 | { // acquire lock |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 159 | std::scoped_lock _l(getLock()); |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 160 | |
| 161 | mInputDeviceInfos = args.inputDeviceInfos; |
| 162 | pointerDisplayChange = updatePointerControllersLocked(); |
| 163 | } // release lock |
| 164 | |
| 165 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 166 | mNextListener.notify(args); |
| 167 | } |
| 168 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 169 | void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) { |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 170 | fadeMouseCursorOnKeyPress(args); |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 171 | mNextListener.notify(args); |
| 172 | } |
| 173 | |
| 174 | void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 175 | NotifyMotionArgs newArgs = processMotion(args); |
| 176 | |
| 177 | mNextListener.notify(newArgs); |
| 178 | } |
| 179 | |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 180 | void PointerChoreographer::fadeMouseCursorOnKeyPress(const android::NotifyKeyArgs& args) { |
| 181 | if (args.action == AKEY_EVENT_ACTION_UP || isMetaKey(args.keyCode)) { |
| 182 | return; |
| 183 | } |
| 184 | // Meta state for these keys is ignored for dismissing cursor while typing |
| 185 | constexpr static int32_t ALLOW_FADING_META_STATE_MASK = AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | |
| 186 | AMETA_SCROLL_LOCK_ON | AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON | AMETA_SHIFT_ON; |
| 187 | if (args.metaState & ~ALLOW_FADING_META_STATE_MASK) { |
| 188 | // Do not fade if any other meta state is active |
| 189 | return; |
| 190 | } |
| 191 | if (!mPolicy.isInputMethodConnectionActive()) { |
| 192 | return; |
| 193 | } |
| 194 | |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 195 | std::scoped_lock _l(getLock()); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 196 | ui::LogicalDisplayId targetDisplay = args.displayId; |
| 197 | if (targetDisplay == ui::LogicalDisplayId::INVALID) { |
| 198 | targetDisplay = mCurrentFocusedDisplay; |
| 199 | } |
| 200 | auto it = mMousePointersByDisplay.find(targetDisplay); |
| 201 | if (it != mMousePointersByDisplay.end()) { |
Arpit Singh | 849beb4 | 2024-06-06 07:14:17 +0000 | [diff] [blame] | 202 | mPolicy.notifyMouseCursorFadedOnTyping(); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 203 | it->second->fade(PointerControllerInterface::Transition::GRADUAL); |
| 204 | } |
| 205 | } |
| 206 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 207 | NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 208 | std::scoped_lock _l(getLock()); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 209 | |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 210 | if (isFromMouse(args)) { |
| 211 | return processMouseEventLocked(args); |
| 212 | } else if (isFromTouchpad(args)) { |
| 213 | return processTouchpadEventLocked(args); |
| 214 | } else if (isFromDrawingTablet(args)) { |
| 215 | processDrawingTabletEventLocked(args); |
| 216 | } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) { |
| 217 | processStylusHoverEventLocked(args); |
| 218 | } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) { |
| 219 | processTouchscreenAndStylusEventLocked(args); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 220 | } |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 221 | return args; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) { |
| 225 | if (args.getPointerCount() != 1) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 226 | LOG(FATAL) << "Only mouse events with a single pointer are currently supported: " |
| 227 | << args.dump(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 228 | } |
| 229 | |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 230 | mMouseDevices.emplace(args.deviceId); |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 231 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 232 | NotifyMotionArgs newArgs(args); |
| 233 | newArgs.displayId = displayId; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 234 | |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 235 | if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) { |
| 236 | // This is an absolute mouse device that knows about the location of the cursor on the |
| 237 | // display, so set the cursor position to the specified location. |
| 238 | const auto [x, y] = pc.getPosition(); |
| 239 | const float deltaX = args.xCursorPosition - x; |
| 240 | const float deltaY = args.yCursorPosition - y; |
| 241 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX); |
| 242 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY); |
| 243 | pc.setPosition(args.xCursorPosition, args.yCursorPosition); |
| 244 | } else { |
| 245 | // This is a relative mouse, so move the cursor by the specified amount. |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 246 | const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X); |
| 247 | const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y); |
| 248 | pc.move(deltaX, deltaY); |
| 249 | const auto [x, y] = pc.getPosition(); |
| 250 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 251 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 252 | newArgs.xCursorPosition = x; |
| 253 | newArgs.yCursorPosition = y; |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 254 | } |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 255 | if (canUnfadeOnDisplay(displayId)) { |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 256 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 257 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 258 | return newArgs; |
| 259 | } |
| 260 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 261 | NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) { |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 262 | mMouseDevices.emplace(args.deviceId); |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 263 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 264 | |
| 265 | NotifyMotionArgs newArgs(args); |
| 266 | newArgs.displayId = displayId; |
| 267 | if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) { |
| 268 | // This is a movement of the mouse pointer. |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 269 | const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X); |
| 270 | const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y); |
| 271 | pc.move(deltaX, deltaY); |
| 272 | if (canUnfadeOnDisplay(displayId)) { |
| 273 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 274 | } |
| 275 | |
| 276 | const auto [x, y] = pc.getPosition(); |
| 277 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 278 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 279 | newArgs.xCursorPosition = x; |
| 280 | newArgs.yCursorPosition = y; |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 281 | } else { |
| 282 | // This is a trackpad gesture with fake finger(s) that should not move the mouse pointer. |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 283 | if (canUnfadeOnDisplay(displayId)) { |
| 284 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 285 | } |
| 286 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 287 | const auto [x, y] = pc.getPosition(); |
| 288 | for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) { |
| 289 | newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 290 | args.pointerCoords[i].getX() + x); |
| 291 | newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 292 | args.pointerCoords[i].getY() + y); |
| 293 | } |
| 294 | newArgs.xCursorPosition = x; |
| 295 | newArgs.yCursorPosition = y; |
| 296 | } |
| 297 | return newArgs; |
| 298 | } |
| 299 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 300 | void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 301 | if (args.displayId == ui::LogicalDisplayId::INVALID) { |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
| 305 | if (args.getPointerCount() != 1) { |
| 306 | LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: " |
| 307 | << args.dump(); |
| 308 | } |
| 309 | |
| 310 | // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist. |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 311 | auto [it, controllerAdded] = |
| 312 | mDrawingTabletPointersByDevice.try_emplace(args.deviceId, |
| 313 | getMouseControllerConstructor( |
| 314 | args.displayId)); |
| 315 | if (controllerAdded) { |
| 316 | onControllerAddedOrRemovedLocked(); |
| 317 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 318 | |
| 319 | PointerControllerInterface& pc = *it->second; |
| 320 | |
| 321 | const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X); |
| 322 | const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 323 | pc.setPosition(x, y); |
| 324 | if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 325 | // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed |
| 326 | // immediately by a DOWN event. |
| 327 | pc.fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 328 | pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED); |
| 329 | } else if (canUnfadeOnDisplay(args.displayId)) { |
| 330 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 331 | } |
| 332 | } |
| 333 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 334 | /** |
| 335 | * When screen is touched, fade the mouse pointer on that display. We only call fade for |
| 336 | * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the |
| 337 | * mouse device keeps moving and unfades the cursor. |
| 338 | * For touch events, we do not need to populate the cursor position. |
| 339 | */ |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 340 | void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 341 | if (!args.displayId.isValid()) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 345 | if (const auto it = mMousePointersByDisplay.find(args.displayId); |
| 346 | it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) { |
| 347 | it->second->fade(PointerControllerInterface::Transition::GRADUAL); |
| 348 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 349 | |
| 350 | if (!mShowTouchesEnabled) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | // Get the touch pointer controller for the device, or create one if it doesn't exist. |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 355 | auto [it, controllerAdded] = |
| 356 | mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor); |
| 357 | if (controllerAdded) { |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 358 | onControllerAddedOrRemovedLocked(); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 359 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 360 | |
| 361 | PointerControllerInterface& pc = *it->second; |
| 362 | |
| 363 | const PointerCoords* coords = args.pointerCoords.data(); |
| 364 | const int32_t maskedAction = MotionEvent::getActionMasked(args.action); |
| 365 | const uint8_t actionIndex = MotionEvent::getActionIndex(args.action); |
| 366 | std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex; |
| 367 | BitSet32 idBits; |
Linnan Li | 45b321e | 2024-07-17 19:33:21 +0000 | [diff] [blame] | 368 | if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL && |
| 369 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 370 | for (size_t i = 0; i < args.getPointerCount(); i++) { |
| 371 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) { |
| 372 | continue; |
| 373 | } |
| 374 | uint32_t id = args.pointerProperties[i].id; |
| 375 | idToIndex[id] = i; |
| 376 | idBits.markBit(id); |
| 377 | } |
| 378 | } |
| 379 | // The PointerController already handles setting spots per-display, so |
| 380 | // we do not need to manually manage display changes for touch spots for now. |
| 381 | pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId); |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 384 | void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 385 | if (!args.displayId.isValid()) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (args.getPointerCount() != 1) { |
| 390 | LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: " |
| 391 | << args.dump(); |
| 392 | } |
| 393 | |
| 394 | // Get the stylus pointer controller for the device, or create one if it doesn't exist. |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 395 | auto [it, controllerAdded] = |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 396 | mStylusPointersByDevice.try_emplace(args.deviceId, |
| 397 | getStylusControllerConstructor(args.displayId)); |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 398 | if (controllerAdded) { |
| 399 | onControllerAddedOrRemovedLocked(); |
| 400 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 401 | |
| 402 | PointerControllerInterface& pc = *it->second; |
| 403 | |
| 404 | const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X); |
| 405 | const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 406 | pc.setPosition(x, y); |
| 407 | if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 408 | // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed |
| 409 | // immediately by a DOWN event. |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 410 | pc.fade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | 888993d | 2024-09-17 20:01:48 +0000 | [diff] [blame] | 411 | pc.updatePointerIcon(mShowTouchesEnabled ? PointerIconStyle::TYPE_SPOT_HOVER |
| 412 | : PointerIconStyle::TYPE_NOT_SPECIFIED); |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 413 | } else if (canUnfadeOnDisplay(args.displayId)) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 414 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 415 | } |
| 416 | } |
| 417 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 418 | void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) { |
| 419 | mNextListener.notify(args); |
| 420 | } |
| 421 | |
| 422 | void PointerChoreographer::notifySensor(const NotifySensorArgs& args) { |
| 423 | mNextListener.notify(args); |
| 424 | } |
| 425 | |
| 426 | void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
| 427 | mNextListener.notify(args); |
| 428 | } |
| 429 | |
| 430 | void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 431 | processDeviceReset(args); |
| 432 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 433 | mNextListener.notify(args); |
| 434 | } |
| 435 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 436 | void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 437 | std::scoped_lock _l(getLock()); |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 438 | mTouchPointersByDevice.erase(args.deviceId); |
| 439 | mStylusPointersByDevice.erase(args.deviceId); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 440 | mDrawingTabletPointersByDevice.erase(args.deviceId); |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 441 | onControllerAddedOrRemovedLocked(); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 444 | void PointerChoreographer::onControllerAddedOrRemovedLocked() { |
Liana Kazanova (xWF) | ff80e30 | 2024-11-18 19:14:42 +0000 | [diff] [blame] | 445 | if (!com::android::input::flags::hide_pointer_indicators_for_secure_windows()) { |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 446 | return; |
| 447 | } |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 448 | bool requireListener = !mTouchPointersByDevice.empty() || !mMousePointersByDisplay.empty() || |
| 449 | !mDrawingTabletPointersByDevice.empty() || !mStylusPointersByDevice.empty(); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 450 | |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 451 | // PointerChoreographer uses Listener's lock which is already held by caller |
| 452 | base::ScopedLockAssertion assumeLocked(mWindowInfoListener->mLock); |
| 453 | |
| 454 | if (requireListener && !mIsWindowInfoListenerRegistered) { |
| 455 | mIsWindowInfoListenerRegistered = true; |
| 456 | mWindowInfoListener->setInitialDisplayInfosLocked(mRegisterListener(mWindowInfoListener)); |
| 457 | onPrivacySensitiveDisplaysChangedLocked( |
| 458 | mWindowInfoListener->getPrivacySensitiveDisplaysLocked()); |
| 459 | } else if (!requireListener && mIsWindowInfoListenerRegistered) { |
| 460 | mIsWindowInfoListenerRegistered = false; |
Arpit Singh | bd49b28 | 2024-05-23 18:02:54 +0000 | [diff] [blame] | 461 | mUnregisterListener(mWindowInfoListener); |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 462 | } else if (requireListener) { |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 463 | // controller may have been added to an existing privacy sensitive display, we need to |
| 464 | // update all controllers again |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 465 | onPrivacySensitiveDisplaysChangedLocked( |
| 466 | mWindowInfoListener->getPrivacySensitiveDisplaysLocked()); |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | void PointerChoreographer::onPrivacySensitiveDisplaysChangedLocked( |
| 471 | const std::unordered_set<ui::LogicalDisplayId>& privacySensitiveDisplays) { |
| 472 | for (auto& [_, pc] : mTouchPointersByDevice) { |
| 473 | pc->clearSkipScreenshotFlags(); |
| 474 | for (auto displayId : privacySensitiveDisplays) { |
| 475 | pc->setSkipScreenshotFlagForDisplay(displayId); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | for (auto& [displayId, pc] : mMousePointersByDisplay) { |
| 480 | if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) { |
| 481 | pc->setSkipScreenshotFlagForDisplay(displayId); |
| 482 | } else { |
| 483 | pc->clearSkipScreenshotFlags(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | for (auto* pointerControllerByDevice : |
| 488 | {&mDrawingTabletPointersByDevice, &mStylusPointersByDevice}) { |
| 489 | for (auto& [_, pc] : *pointerControllerByDevice) { |
| 490 | auto displayId = pc->getDisplayId(); |
| 491 | if (privacySensitiveDisplays.find(displayId) != privacySensitiveDisplays.end()) { |
| 492 | pc->setSkipScreenshotFlagForDisplay(displayId); |
| 493 | } else { |
| 494 | pc->clearSkipScreenshotFlags(); |
| 495 | } |
| 496 | } |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 497 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 498 | } |
| 499 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 500 | void PointerChoreographer::notifyPointerCaptureChanged( |
| 501 | const NotifyPointerCaptureChangedArgs& args) { |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 502 | if (args.request.isEnable()) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 503 | std::scoped_lock _l(getLock()); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 504 | for (const auto& [_, mousePointerController] : mMousePointersByDisplay) { |
| 505 | mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 506 | } |
| 507 | } |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 508 | mNextListener.notify(args); |
| 509 | } |
| 510 | |
| 511 | void PointerChoreographer::dump(std::string& dump) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 512 | std::scoped_lock _l(getLock()); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 513 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 514 | dump += "PointerChoreographer:\n"; |
Harry Cutts | ebd418a | 2024-08-16 15:52:24 +0000 | [diff] [blame] | 515 | dump += StringPrintf(INDENT "Show Touches Enabled: %s\n", |
| 516 | mShowTouchesEnabled ? "true" : "false"); |
| 517 | dump += StringPrintf(INDENT "Stylus PointerIcon Enabled: %s\n", |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 518 | mStylusPointerIconEnabled ? "true" : "false"); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 519 | |
| 520 | dump += INDENT "MousePointerControllers:\n"; |
| 521 | for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) { |
| 522 | std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 523 | dump += INDENT + displayId.toString() + " : " + pointerControllerDump; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 524 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 525 | dump += INDENT "TouchPointerControllers:\n"; |
| 526 | for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) { |
| 527 | std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT); |
| 528 | dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump; |
| 529 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 530 | dump += INDENT "StylusPointerControllers:\n"; |
| 531 | for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) { |
| 532 | std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT); |
| 533 | dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump; |
| 534 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 535 | dump += INDENT "DrawingTabletControllers:\n"; |
| 536 | for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) { |
| 537 | std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT); |
| 538 | dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump; |
| 539 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 540 | dump += "\n"; |
| 541 | } |
| 542 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 543 | const DisplayViewport* PointerChoreographer::findViewportByIdLocked( |
| 544 | ui::LogicalDisplayId displayId) const { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 545 | for (auto& viewport : mViewports) { |
| 546 | if (viewport.displayId == displayId) { |
| 547 | return &viewport; |
| 548 | } |
| 549 | } |
| 550 | return nullptr; |
| 551 | } |
| 552 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 553 | ui::LogicalDisplayId PointerChoreographer::getTargetMouseDisplayLocked( |
| 554 | ui::LogicalDisplayId associatedDisplayId) const { |
| 555 | return associatedDisplayId.isValid() ? associatedDisplayId : mDefaultMouseDisplayId; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 556 | } |
| 557 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 558 | std::pair<ui::LogicalDisplayId, PointerControllerInterface&> |
| 559 | PointerChoreographer::ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) { |
| 560 | const ui::LogicalDisplayId displayId = getTargetMouseDisplayLocked(associatedDisplayId); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 561 | |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 562 | auto it = mMousePointersByDisplay.find(displayId); |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 563 | if (it == mMousePointersByDisplay.end()) { |
| 564 | it = mMousePointersByDisplay.emplace(displayId, getMouseControllerConstructor(displayId)) |
| 565 | .first; |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 566 | onControllerAddedOrRemovedLocked(); |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 567 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 568 | |
| 569 | return {displayId, *it->second}; |
| 570 | } |
| 571 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 572 | InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 573 | auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(), |
| 574 | [deviceId](const auto& info) { return info.getId() == deviceId; }); |
| 575 | return it != mInputDeviceInfos.end() ? &(*it) : nullptr; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 576 | } |
| 577 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 578 | bool PointerChoreographer::canUnfadeOnDisplay(ui::LogicalDisplayId displayId) { |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 579 | return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end(); |
| 580 | } |
| 581 | |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 582 | std::mutex& PointerChoreographer::getLock() const { |
| 583 | return mWindowInfoListener->mLock; |
| 584 | } |
| 585 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 586 | PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 587 | std::set<ui::LogicalDisplayId /*displayId*/> mouseDisplaysToKeep; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 588 | std::set<DeviceId> touchDevicesToKeep; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 589 | std::set<DeviceId> stylusDevicesToKeep; |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 590 | std::set<DeviceId> drawingTabletDevicesToKeep; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 591 | |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 592 | // Mark the displayIds or deviceIds of PointerControllers currently needed, and create |
| 593 | // new PointerControllers if necessary. |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 594 | for (const auto& info : mInputDeviceInfos) { |
Linnan Li | 48f80da | 2024-04-22 18:38:16 +0000 | [diff] [blame] | 595 | if (!info.isEnabled()) { |
| 596 | // If device is disabled, we should not keep it, and should not show pointer for |
| 597 | // disabled mouse device. |
| 598 | continue; |
| 599 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 600 | const uint32_t sources = info.getSources(); |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 601 | const bool isKnownMouse = mMouseDevices.count(info.getId()) != 0; |
| 602 | |
| 603 | if (isMouseOrTouchpad(sources) || isKnownMouse) { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 604 | const ui::LogicalDisplayId displayId = |
| 605 | getTargetMouseDisplayLocked(info.getAssociatedDisplayId()); |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 606 | mouseDisplaysToKeep.insert(displayId); |
| 607 | // For mice, show the cursor immediately when the device is first connected or |
| 608 | // when it moves to a new display. |
| 609 | auto [mousePointerIt, isNewMousePointer] = |
| 610 | mMousePointersByDisplay.try_emplace(displayId, |
| 611 | getMouseControllerConstructor(displayId)); |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 612 | if (isNewMousePointer) { |
| 613 | onControllerAddedOrRemovedLocked(); |
| 614 | } |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 615 | |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 616 | mMouseDevices.emplace(info.getId()); |
| 617 | if ((!isKnownMouse || isNewMousePointer) && canUnfadeOnDisplay(displayId)) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 618 | mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 619 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 620 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 621 | if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled && |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 622 | info.getAssociatedDisplayId().isValid()) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 623 | touchDevicesToKeep.insert(info.getId()); |
| 624 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 625 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled && |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 626 | info.getAssociatedDisplayId().isValid()) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 627 | stylusDevicesToKeep.insert(info.getId()); |
| 628 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 629 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) && |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 630 | info.getAssociatedDisplayId().isValid()) { |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 631 | drawingTabletDevicesToKeep.insert(info.getId()); |
| 632 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // Remove PointerControllers no longer needed. |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 636 | std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 637 | return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 638 | }); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 639 | std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 640 | return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end(); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 641 | }); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 642 | std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 643 | return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end(); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 644 | }); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 645 | std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) { |
| 646 | return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end(); |
| 647 | }); |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 648 | std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(getLock()) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 649 | return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(), |
| 650 | [id](const auto& info) { return info.getId() == id; }) == |
| 651 | mInputDeviceInfos.end(); |
| 652 | }); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 653 | |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 654 | onControllerAddedOrRemovedLocked(); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 655 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 656 | // Check if we need to notify the policy if there's a change on the pointer display ID. |
| 657 | return calculatePointerDisplayChangeToNotify(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 658 | } |
| 659 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 660 | PointerChoreographer::PointerDisplayChange |
| 661 | PointerChoreographer::calculatePointerDisplayChangeToNotify() { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 662 | ui::LogicalDisplayId displayIdToNotify = ui::LogicalDisplayId::INVALID; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 663 | FloatPoint cursorPosition = {0, 0}; |
| 664 | if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId); |
| 665 | it != mMousePointersByDisplay.end()) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 666 | const auto& pointerController = it->second; |
| 667 | // Use the displayId from the pointerController, because it accurately reflects whether |
| 668 | // the viewport has been added for that display. Otherwise, we would have to check if |
| 669 | // the viewport exists separately. |
| 670 | displayIdToNotify = pointerController->getDisplayId(); |
| 671 | cursorPosition = pointerController->getPosition(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 672 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 673 | if (mNotifiedPointerDisplayId == displayIdToNotify) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 674 | return {}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 675 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 676 | mNotifiedPointerDisplayId = displayIdToNotify; |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 677 | return {{displayIdToNotify, cursorPosition}}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 678 | } |
| 679 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 680 | void PointerChoreographer::setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 681 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 682 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 683 | { // acquire lock |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 684 | std::scoped_lock _l(getLock()); |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 685 | |
| 686 | mDefaultMouseDisplayId = displayId; |
| 687 | pointerDisplayChange = updatePointerControllersLocked(); |
| 688 | } // release lock |
| 689 | |
| 690 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 694 | PointerDisplayChange pointerDisplayChange; |
| 695 | |
| 696 | { // acquire lock |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 697 | std::scoped_lock _l(getLock()); |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 698 | for (const auto& viewport : viewports) { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 699 | const ui::LogicalDisplayId displayId = viewport.displayId; |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 700 | if (const auto it = mMousePointersByDisplay.find(displayId); |
| 701 | it != mMousePointersByDisplay.end()) { |
| 702 | it->second->setDisplayViewport(viewport); |
| 703 | } |
| 704 | for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) { |
| 705 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 706 | if (info && info->getAssociatedDisplayId() == displayId) { |
| 707 | stylusPointerController->setDisplayViewport(viewport); |
| 708 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 709 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 710 | for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) { |
| 711 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 712 | if (info && info->getAssociatedDisplayId() == displayId) { |
| 713 | drawingTabletController->setDisplayViewport(viewport); |
| 714 | } |
| 715 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 716 | } |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 717 | mViewports = viewports; |
| 718 | pointerDisplayChange = calculatePointerDisplayChangeToNotify(); |
| 719 | } // release lock |
| 720 | |
| 721 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 725 | ui::LogicalDisplayId associatedDisplayId) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 726 | std::scoped_lock _l(getLock()); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 727 | const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 728 | if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) { |
| 729 | return *viewport; |
| 730 | } |
| 731 | return std::nullopt; |
| 732 | } |
| 733 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 734 | FloatPoint PointerChoreographer::getMouseCursorPosition(ui::LogicalDisplayId displayId) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 735 | std::scoped_lock _l(getLock()); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 736 | const ui::LogicalDisplayId resolvedDisplayId = getTargetMouseDisplayLocked(displayId); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 737 | if (auto it = mMousePointersByDisplay.find(resolvedDisplayId); |
| 738 | it != mMousePointersByDisplay.end()) { |
| 739 | return it->second->getPosition(); |
| 740 | } |
| 741 | return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 744 | void PointerChoreographer::setShowTouchesEnabled(bool enabled) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 745 | PointerDisplayChange pointerDisplayChange; |
| 746 | |
| 747 | { // acquire lock |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 748 | std::scoped_lock _l(getLock()); |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 749 | if (mShowTouchesEnabled == enabled) { |
| 750 | return; |
| 751 | } |
| 752 | mShowTouchesEnabled = enabled; |
| 753 | pointerDisplayChange = updatePointerControllersLocked(); |
| 754 | } // release lock |
| 755 | |
| 756 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 757 | } |
| 758 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 759 | void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 760 | PointerDisplayChange pointerDisplayChange; |
| 761 | |
| 762 | { // acquire lock |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 763 | std::scoped_lock _l(getLock()); |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 764 | if (mStylusPointerIconEnabled == enabled) { |
| 765 | return; |
| 766 | } |
| 767 | mStylusPointerIconEnabled = enabled; |
| 768 | pointerDisplayChange = updatePointerControllersLocked(); |
| 769 | } // release lock |
| 770 | |
| 771 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 772 | } |
| 773 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 774 | bool PointerChoreographer::setPointerIcon( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 775 | std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, |
| 776 | ui::LogicalDisplayId displayId, DeviceId deviceId) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 777 | std::scoped_lock _l(getLock()); |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 778 | if (deviceId < 0) { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 779 | LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 780 | return false; |
| 781 | } |
| 782 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 783 | if (!info) { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 784 | LOG(WARNING) << "No input device info found for id " << deviceId |
| 785 | << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | const uint32_t sources = info->getSources(); |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 789 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 790 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) { |
| 791 | auto it = mDrawingTabletPointersByDevice.find(deviceId); |
| 792 | if (it != mDrawingTabletPointersByDevice.end()) { |
| 793 | setIconForController(icon, *it->second); |
| 794 | return true; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 795 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 796 | } |
| 797 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) { |
| 798 | auto it = mStylusPointersByDevice.find(deviceId); |
| 799 | if (it != mStylusPointersByDevice.end()) { |
Prabir Pradhan | 888993d | 2024-09-17 20:01:48 +0000 | [diff] [blame] | 800 | if (mShowTouchesEnabled) { |
| 801 | // If an app doesn't override the icon for the hovering stylus, show the hover icon. |
| 802 | auto* style = std::get_if<PointerIconStyle>(&icon); |
| 803 | if (style != nullptr && *style == PointerIconStyle::TYPE_NOT_SPECIFIED) { |
| 804 | *style = PointerIconStyle::TYPE_SPOT_HOVER; |
| 805 | } |
| 806 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 807 | setIconForController(icon, *it->second); |
| 808 | return true; |
| 809 | } |
| 810 | } |
| 811 | if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) { |
| 812 | auto it = mMousePointersByDisplay.find(displayId); |
| 813 | if (it != mMousePointersByDisplay.end()) { |
| 814 | setIconForController(icon, *it->second); |
| 815 | return true; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 816 | } else { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 817 | LOG(WARNING) << "No mouse pointer controller found for display " << displayId |
| 818 | << ", device " << deviceId << "."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 819 | return false; |
| 820 | } |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 821 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 822 | LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId |
| 823 | << "."; |
| 824 | return false; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 825 | } |
| 826 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 827 | void PointerChoreographer::setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 828 | std::scoped_lock lock(getLock()); |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 829 | if (visible) { |
| 830 | mDisplaysWithPointersHidden.erase(displayId); |
| 831 | // We do not unfade the icons here, because we don't know when the last event happened. |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | mDisplaysWithPointersHidden.emplace(displayId); |
| 836 | |
| 837 | // Hide any icons that are currently visible on the display. |
| 838 | if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) { |
| 839 | const auto& [_, controller] = *it; |
| 840 | controller->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 841 | } |
| 842 | for (const auto& [_, controller] : mStylusPointersByDevice) { |
| 843 | if (controller->getDisplayId() == displayId) { |
| 844 | controller->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 849 | void PointerChoreographer::setFocusedDisplay(ui::LogicalDisplayId displayId) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 850 | std::scoped_lock lock(getLock()); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 851 | mCurrentFocusedDisplay = displayId; |
| 852 | } |
| 853 | |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 854 | PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 855 | ui::LogicalDisplayId displayId) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 856 | std::function<std::shared_ptr<PointerControllerInterface>()> ctor = |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 857 | [this, displayId]() REQUIRES(getLock()) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 858 | auto pc = mPolicy.createPointerController( |
| 859 | PointerControllerInterface::ControllerType::MOUSE); |
| 860 | if (const auto viewport = findViewportByIdLocked(displayId); viewport) { |
| 861 | pc->setDisplayViewport(*viewport); |
| 862 | } |
| 863 | return pc; |
| 864 | }; |
| 865 | return ConstructorDelegate(std::move(ctor)); |
| 866 | } |
| 867 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 868 | PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 869 | ui::LogicalDisplayId displayId) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 870 | std::function<std::shared_ptr<PointerControllerInterface>()> ctor = |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 871 | [this, displayId]() REQUIRES(getLock()) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 872 | auto pc = mPolicy.createPointerController( |
| 873 | PointerControllerInterface::ControllerType::STYLUS); |
| 874 | if (const auto viewport = findViewportByIdLocked(displayId); viewport) { |
| 875 | pc->setDisplayViewport(*viewport); |
| 876 | } |
| 877 | return pc; |
| 878 | }; |
| 879 | return ConstructorDelegate(std::move(ctor)); |
| 880 | } |
| 881 | |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 882 | // --- PointerChoreographer::PointerChoreographerDisplayInfoListener --- |
| 883 | |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 884 | void PointerChoreographer::PointerChoreographerDisplayInfoListener::onWindowInfosChanged( |
| 885 | const gui::WindowInfosUpdate& windowInfosUpdate) { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 886 | std::scoped_lock _l(mLock); |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 887 | if (mPointerChoreographer == nullptr) { |
| 888 | return; |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 889 | } |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 890 | auto newPrivacySensitiveDisplays = |
| 891 | getPrivacySensitiveDisplaysFromWindowInfos(windowInfosUpdate.windowInfos); |
| 892 | if (newPrivacySensitiveDisplays != mPrivacySensitiveDisplays) { |
| 893 | mPrivacySensitiveDisplays = std::move(newPrivacySensitiveDisplays); |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 894 | // PointerChoreographer uses Listener's lock. |
| 895 | base::ScopedLockAssertion assumeLocked(mPointerChoreographer->getLock()); |
| 896 | mPointerChoreographer->onPrivacySensitiveDisplaysChangedLocked(mPrivacySensitiveDisplays); |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 900 | void PointerChoreographer::PointerChoreographerDisplayInfoListener::setInitialDisplayInfosLocked( |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 901 | const std::vector<gui::WindowInfo>& windowInfos) { |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 902 | mPrivacySensitiveDisplays = getPrivacySensitiveDisplaysFromWindowInfos(windowInfos); |
| 903 | } |
| 904 | |
| 905 | std::unordered_set<ui::LogicalDisplayId /*displayId*/> |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 906 | PointerChoreographer::PointerChoreographerDisplayInfoListener::getPrivacySensitiveDisplaysLocked() { |
Arpit Singh | 420d074 | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 907 | return mPrivacySensitiveDisplays; |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | void PointerChoreographer::PointerChoreographerDisplayInfoListener:: |
| 911 | onPointerChoreographerDestroyed() { |
Arpit Singh | 7918c82 | 2024-11-20 11:28:44 +0000 | [diff] [blame^] | 912 | std::scoped_lock _l(mLock); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 913 | mPointerChoreographer = nullptr; |
| 914 | } |
| 915 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 916 | } // namespace android |