Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "PointerChoreographer" |
| 18 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <input/PrintTools.h> |
| 21 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 22 | #include "PointerChoreographer.h" |
| 23 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 24 | #define INDENT " " |
| 25 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 28 | namespace { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 29 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 30 | bool isFromMouse(const NotifyMotionArgs& args) { |
| 31 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE) && |
| 32 | args.pointerProperties[0].toolType == ToolType::MOUSE; |
| 33 | } |
| 34 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 35 | bool isFromTouchpad(const NotifyMotionArgs& args) { |
| 36 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE) && |
| 37 | args.pointerProperties[0].toolType == ToolType::FINGER; |
| 38 | } |
| 39 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 40 | bool isFromDrawingTablet(const NotifyMotionArgs& args) { |
| 41 | return isFromSource(args.source, AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS) && |
| 42 | isStylusToolType(args.pointerProperties[0].toolType); |
| 43 | } |
| 44 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 45 | bool isHoverAction(int32_t action) { |
| 46 | return action == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 47 | action == AMOTION_EVENT_ACTION_HOVER_MOVE || action == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 48 | } |
| 49 | |
| 50 | bool isStylusHoverEvent(const NotifyMotionArgs& args) { |
| 51 | return isStylusEvent(args.source, args.pointerProperties) && isHoverAction(args.action); |
| 52 | } |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 53 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 54 | bool isMouseOrTouchpad(uint32_t sources) { |
| 55 | // Check if this is a mouse or touchpad, but not a drawing tablet. |
| 56 | return isFromSource(sources, AINPUT_SOURCE_MOUSE_RELATIVE) || |
| 57 | (isFromSource(sources, AINPUT_SOURCE_MOUSE) && |
| 58 | !isFromSource(sources, AINPUT_SOURCE_STYLUS)); |
| 59 | } |
| 60 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 61 | inline void notifyPointerDisplayChange(std::optional<std::tuple<int32_t, FloatPoint>> change, |
| 62 | PointerChoreographerPolicyInterface& policy) { |
| 63 | if (!change) { |
| 64 | return; |
| 65 | } |
| 66 | const auto& [displayId, cursorPosition] = *change; |
| 67 | policy.notifyPointerDisplayIdChanged(displayId, cursorPosition); |
| 68 | } |
| 69 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 70 | void setIconForController(const std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle>& icon, |
| 71 | PointerControllerInterface& controller) { |
| 72 | if (std::holds_alternative<std::unique_ptr<SpriteIcon>>(icon)) { |
| 73 | if (std::get<std::unique_ptr<SpriteIcon>>(icon) == nullptr) { |
| 74 | LOG(FATAL) << "SpriteIcon should not be null"; |
| 75 | } |
| 76 | controller.setCustomPointerIcon(*std::get<std::unique_ptr<SpriteIcon>>(icon)); |
| 77 | } else { |
| 78 | controller.updatePointerIcon(std::get<PointerIconStyle>(icon)); |
| 79 | } |
| 80 | } |
| 81 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 82 | } // namespace |
| 83 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 84 | // --- PointerChoreographer --- |
| 85 | |
| 86 | PointerChoreographer::PointerChoreographer(InputListenerInterface& listener, |
| 87 | PointerChoreographerPolicyInterface& policy) |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 88 | : mTouchControllerConstructor([this]() { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 89 | return mPolicy.createPointerController( |
| 90 | PointerControllerInterface::ControllerType::TOUCH); |
| 91 | }), |
| 92 | mNextListener(listener), |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 93 | mPolicy(policy), |
| 94 | mDefaultMouseDisplayId(ADISPLAY_ID_DEFAULT), |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 95 | mNotifiedPointerDisplayId(ADISPLAY_ID_NONE), |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 96 | mShowTouchesEnabled(false), |
| 97 | mStylusPointerIconEnabled(false) {} |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 98 | |
| 99 | void PointerChoreographer::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 100 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 101 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 102 | { // acquire lock |
| 103 | std::scoped_lock _l(mLock); |
| 104 | |
| 105 | mInputDeviceInfos = args.inputDeviceInfos; |
| 106 | pointerDisplayChange = updatePointerControllersLocked(); |
| 107 | } // release lock |
| 108 | |
| 109 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 110 | mNextListener.notify(args); |
| 111 | } |
| 112 | |
| 113 | void PointerChoreographer::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
| 114 | mNextListener.notify(args); |
| 115 | } |
| 116 | |
| 117 | void PointerChoreographer::notifyKey(const NotifyKeyArgs& args) { |
| 118 | mNextListener.notify(args); |
| 119 | } |
| 120 | |
| 121 | void PointerChoreographer::notifyMotion(const NotifyMotionArgs& args) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 122 | NotifyMotionArgs newArgs = processMotion(args); |
| 123 | |
| 124 | mNextListener.notify(newArgs); |
| 125 | } |
| 126 | |
| 127 | NotifyMotionArgs PointerChoreographer::processMotion(const NotifyMotionArgs& args) { |
| 128 | std::scoped_lock _l(mLock); |
| 129 | |
| 130 | if (isFromMouse(args)) { |
| 131 | return processMouseEventLocked(args); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 132 | } else if (isFromTouchpad(args)) { |
| 133 | return processTouchpadEventLocked(args); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 134 | } else if (isFromDrawingTablet(args)) { |
| 135 | processDrawingTabletEventLocked(args); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 136 | } else if (mStylusPointerIconEnabled && isStylusHoverEvent(args)) { |
| 137 | processStylusHoverEventLocked(args); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 138 | } else if (isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN)) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 139 | processTouchscreenAndStylusEventLocked(args); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 140 | } |
| 141 | return args; |
| 142 | } |
| 143 | |
| 144 | NotifyMotionArgs PointerChoreographer::processMouseEventLocked(const NotifyMotionArgs& args) { |
| 145 | if (args.getPointerCount() != 1) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 146 | LOG(FATAL) << "Only mouse events with a single pointer are currently supported: " |
| 147 | << args.dump(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 150 | mMouseDevices.emplace(args.deviceId); |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 151 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 152 | NotifyMotionArgs newArgs(args); |
| 153 | newArgs.displayId = displayId; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 154 | |
Nergi Rahardi | e0a4cfe | 2024-03-11 13:18:59 +0900 | [diff] [blame] | 155 | if (MotionEvent::isValidCursorPosition(args.xCursorPosition, args.yCursorPosition)) { |
| 156 | // This is an absolute mouse device that knows about the location of the cursor on the |
| 157 | // display, so set the cursor position to the specified location. |
| 158 | const auto [x, y] = pc.getPosition(); |
| 159 | const float deltaX = args.xCursorPosition - x; |
| 160 | const float deltaY = args.yCursorPosition - y; |
| 161 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, deltaX); |
| 162 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, deltaY); |
| 163 | pc.setPosition(args.xCursorPosition, args.yCursorPosition); |
| 164 | } else { |
| 165 | // This is a relative mouse, so move the cursor by the specified amount. |
| 166 | const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X); |
| 167 | const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y); |
| 168 | pc.move(deltaX, deltaY); |
| 169 | const auto [x, y] = pc.getPosition(); |
| 170 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 171 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 172 | newArgs.xCursorPosition = x; |
| 173 | newArgs.yCursorPosition = y; |
| 174 | } |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 175 | if (canUnfadeOnDisplay(displayId)) { |
| 176 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 177 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 178 | return newArgs; |
| 179 | } |
| 180 | |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 181 | NotifyMotionArgs PointerChoreographer::processTouchpadEventLocked(const NotifyMotionArgs& args) { |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 182 | mMouseDevices.emplace(args.deviceId); |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 183 | auto [displayId, pc] = ensureMouseControllerLocked(args.displayId); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 184 | |
| 185 | NotifyMotionArgs newArgs(args); |
| 186 | newArgs.displayId = displayId; |
| 187 | if (args.getPointerCount() == 1 && args.classification == MotionClassification::NONE) { |
| 188 | // This is a movement of the mouse pointer. |
| 189 | const float deltaX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X); |
| 190 | const float deltaY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y); |
| 191 | pc.move(deltaX, deltaY); |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 192 | if (canUnfadeOnDisplay(displayId)) { |
| 193 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 194 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 195 | |
| 196 | const auto [x, y] = pc.getPosition(); |
| 197 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 198 | newArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 199 | newArgs.xCursorPosition = x; |
| 200 | newArgs.yCursorPosition = y; |
| 201 | } else { |
| 202 | // 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] | 203 | if (canUnfadeOnDisplay(displayId)) { |
| 204 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 205 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 206 | |
| 207 | const auto [x, y] = pc.getPosition(); |
| 208 | for (uint32_t i = 0; i < newArgs.getPointerCount(); i++) { |
| 209 | newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 210 | args.pointerCoords[i].getX() + x); |
| 211 | newArgs.pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 212 | args.pointerCoords[i].getY() + y); |
| 213 | } |
| 214 | newArgs.xCursorPosition = x; |
| 215 | newArgs.yCursorPosition = y; |
| 216 | } |
| 217 | return newArgs; |
| 218 | } |
| 219 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 220 | void PointerChoreographer::processDrawingTabletEventLocked(const android::NotifyMotionArgs& args) { |
| 221 | if (args.displayId == ADISPLAY_ID_NONE) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | if (args.getPointerCount() != 1) { |
| 226 | LOG(WARNING) << "Only drawing tablet events with a single pointer are currently supported: " |
| 227 | << args.dump(); |
| 228 | } |
| 229 | |
| 230 | // Use a mouse pointer controller for drawing tablets, or create one if it doesn't exist. |
| 231 | auto [it, _] = mDrawingTabletPointersByDevice.try_emplace(args.deviceId, |
| 232 | getMouseControllerConstructor( |
| 233 | args.displayId)); |
| 234 | |
| 235 | PointerControllerInterface& pc = *it->second; |
| 236 | |
| 237 | const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X); |
| 238 | const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 239 | pc.setPosition(x, y); |
| 240 | if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 241 | // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed |
| 242 | // immediately by a DOWN event. |
| 243 | pc.fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 244 | pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED); |
| 245 | } else if (canUnfadeOnDisplay(args.displayId)) { |
| 246 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 247 | } |
| 248 | } |
| 249 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 250 | /** |
| 251 | * When screen is touched, fade the mouse pointer on that display. We only call fade for |
| 252 | * ACTION_DOWN events.This would allow both mouse and touch to be used at the same time if the |
| 253 | * mouse device keeps moving and unfades the cursor. |
| 254 | * For touch events, we do not need to populate the cursor position. |
| 255 | */ |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 256 | void PointerChoreographer::processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) { |
| 257 | if (args.displayId == ADISPLAY_ID_NONE) { |
| 258 | return; |
| 259 | } |
| 260 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 261 | if (const auto it = mMousePointersByDisplay.find(args.displayId); |
| 262 | it != mMousePointersByDisplay.end() && args.action == AMOTION_EVENT_ACTION_DOWN) { |
| 263 | it->second->fade(PointerControllerInterface::Transition::GRADUAL); |
| 264 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 265 | |
| 266 | if (!mShowTouchesEnabled) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // 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] | 271 | auto [it, _] = mTouchPointersByDevice.try_emplace(args.deviceId, mTouchControllerConstructor); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 272 | |
| 273 | PointerControllerInterface& pc = *it->second; |
| 274 | |
| 275 | const PointerCoords* coords = args.pointerCoords.data(); |
| 276 | const int32_t maskedAction = MotionEvent::getActionMasked(args.action); |
| 277 | const uint8_t actionIndex = MotionEvent::getActionIndex(args.action); |
| 278 | std::array<uint32_t, MAX_POINTER_ID + 1> idToIndex; |
| 279 | BitSet32 idBits; |
| 280 | if (maskedAction != AMOTION_EVENT_ACTION_UP && maskedAction != AMOTION_EVENT_ACTION_CANCEL) { |
| 281 | for (size_t i = 0; i < args.getPointerCount(); i++) { |
| 282 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP && actionIndex == i) { |
| 283 | continue; |
| 284 | } |
| 285 | uint32_t id = args.pointerProperties[i].id; |
| 286 | idToIndex[id] = i; |
| 287 | idBits.markBit(id); |
| 288 | } |
| 289 | } |
| 290 | // The PointerController already handles setting spots per-display, so |
| 291 | // we do not need to manually manage display changes for touch spots for now. |
| 292 | pc.setSpots(coords, idToIndex.cbegin(), idBits, args.displayId); |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 295 | void PointerChoreographer::processStylusHoverEventLocked(const NotifyMotionArgs& args) { |
| 296 | if (args.displayId == ADISPLAY_ID_NONE) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if (args.getPointerCount() != 1) { |
| 301 | LOG(WARNING) << "Only stylus hover events with a single pointer are currently supported: " |
| 302 | << args.dump(); |
| 303 | } |
| 304 | |
| 305 | // Get the stylus pointer controller for the device, or create one if it doesn't exist. |
| 306 | auto [it, _] = |
| 307 | mStylusPointersByDevice.try_emplace(args.deviceId, |
| 308 | getStylusControllerConstructor(args.displayId)); |
| 309 | |
| 310 | PointerControllerInterface& pc = *it->second; |
| 311 | |
| 312 | const float x = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X); |
| 313 | const float y = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 314 | pc.setPosition(x, y); |
| 315 | if (args.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 316 | // TODO(b/315815559): Do not fade and reset the icon if the hover exit will be followed |
| 317 | // immediately by a DOWN event. |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 318 | pc.fade(PointerControllerInterface::Transition::IMMEDIATE); |
Prabir Pradhan | 4b36db9 | 2024-01-03 20:56:57 +0000 | [diff] [blame] | 319 | pc.updatePointerIcon(PointerIconStyle::TYPE_NOT_SPECIFIED); |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 320 | } else if (canUnfadeOnDisplay(args.displayId)) { |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 321 | pc.unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 322 | } |
| 323 | } |
| 324 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 325 | void PointerChoreographer::notifySwitch(const NotifySwitchArgs& args) { |
| 326 | mNextListener.notify(args); |
| 327 | } |
| 328 | |
| 329 | void PointerChoreographer::notifySensor(const NotifySensorArgs& args) { |
| 330 | mNextListener.notify(args); |
| 331 | } |
| 332 | |
| 333 | void PointerChoreographer::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
| 334 | mNextListener.notify(args); |
| 335 | } |
| 336 | |
| 337 | void PointerChoreographer::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 338 | processDeviceReset(args); |
| 339 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 340 | mNextListener.notify(args); |
| 341 | } |
| 342 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 343 | void PointerChoreographer::processDeviceReset(const NotifyDeviceResetArgs& args) { |
| 344 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 345 | mTouchPointersByDevice.erase(args.deviceId); |
| 346 | mStylusPointersByDevice.erase(args.deviceId); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 347 | mDrawingTabletPointersByDevice.erase(args.deviceId); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 348 | } |
| 349 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 350 | void PointerChoreographer::notifyPointerCaptureChanged( |
| 351 | const NotifyPointerCaptureChangedArgs& args) { |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 352 | if (args.request.isEnable()) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 353 | std::scoped_lock _l(mLock); |
| 354 | for (const auto& [_, mousePointerController] : mMousePointersByDisplay) { |
| 355 | mousePointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 356 | } |
| 357 | } |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 358 | mNextListener.notify(args); |
| 359 | } |
| 360 | |
| 361 | void PointerChoreographer::dump(std::string& dump) { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 362 | std::scoped_lock _l(mLock); |
| 363 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 364 | dump += "PointerChoreographer:\n"; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 365 | dump += StringPrintf("show touches: %s\n", mShowTouchesEnabled ? "true" : "false"); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 366 | dump += StringPrintf("stylus pointer icon enabled: %s\n", |
| 367 | mStylusPointerIconEnabled ? "true" : "false"); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 368 | |
| 369 | dump += INDENT "MousePointerControllers:\n"; |
| 370 | for (const auto& [displayId, mousePointerController] : mMousePointersByDisplay) { |
| 371 | std::string pointerControllerDump = addLinePrefix(mousePointerController->dump(), INDENT); |
| 372 | dump += INDENT + std::to_string(displayId) + " : " + pointerControllerDump; |
| 373 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 374 | dump += INDENT "TouchPointerControllers:\n"; |
| 375 | for (const auto& [deviceId, touchPointerController] : mTouchPointersByDevice) { |
| 376 | std::string pointerControllerDump = addLinePrefix(touchPointerController->dump(), INDENT); |
| 377 | dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump; |
| 378 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 379 | dump += INDENT "StylusPointerControllers:\n"; |
| 380 | for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) { |
| 381 | std::string pointerControllerDump = addLinePrefix(stylusPointerController->dump(), INDENT); |
| 382 | dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump; |
| 383 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 384 | dump += INDENT "DrawingTabletControllers:\n"; |
| 385 | for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) { |
| 386 | std::string pointerControllerDump = addLinePrefix(drawingTabletController->dump(), INDENT); |
| 387 | dump += INDENT + std::to_string(deviceId) + " : " + pointerControllerDump; |
| 388 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 389 | dump += "\n"; |
| 390 | } |
| 391 | |
| 392 | const DisplayViewport* PointerChoreographer::findViewportByIdLocked(int32_t displayId) const { |
| 393 | for (auto& viewport : mViewports) { |
| 394 | if (viewport.displayId == displayId) { |
| 395 | return &viewport; |
| 396 | } |
| 397 | } |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
| 401 | int32_t PointerChoreographer::getTargetMouseDisplayLocked(int32_t associatedDisplayId) const { |
| 402 | return associatedDisplayId == ADISPLAY_ID_NONE ? mDefaultMouseDisplayId : associatedDisplayId; |
| 403 | } |
| 404 | |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 405 | std::pair<int32_t, PointerControllerInterface&> PointerChoreographer::ensureMouseControllerLocked( |
| 406 | int32_t associatedDisplayId) { |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 407 | const int32_t displayId = getTargetMouseDisplayLocked(associatedDisplayId); |
| 408 | |
Prabir Pradhan | 990d871 | 2024-03-05 00:31:36 +0000 | [diff] [blame] | 409 | auto it = mMousePointersByDisplay.find(displayId); |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 410 | if (it == mMousePointersByDisplay.end()) { |
| 411 | it = mMousePointersByDisplay.emplace(displayId, getMouseControllerConstructor(displayId)) |
| 412 | .first; |
| 413 | } |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 414 | |
| 415 | return {displayId, *it->second}; |
| 416 | } |
| 417 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 418 | InputDeviceInfo* PointerChoreographer::findInputDeviceLocked(DeviceId deviceId) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 419 | auto it = std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(), |
| 420 | [deviceId](const auto& info) { return info.getId() == deviceId; }); |
| 421 | return it != mInputDeviceInfos.end() ? &(*it) : nullptr; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 422 | } |
| 423 | |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 424 | bool PointerChoreographer::canUnfadeOnDisplay(int32_t displayId) { |
| 425 | return mDisplaysWithPointersHidden.find(displayId) == mDisplaysWithPointersHidden.end(); |
| 426 | } |
| 427 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 428 | PointerChoreographer::PointerDisplayChange PointerChoreographer::updatePointerControllersLocked() { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 429 | std::set<int32_t /*displayId*/> mouseDisplaysToKeep; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 430 | std::set<DeviceId> touchDevicesToKeep; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 431 | std::set<DeviceId> stylusDevicesToKeep; |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 432 | std::set<DeviceId> drawingTabletDevicesToKeep; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 433 | |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 434 | // Mark the displayIds or deviceIds of PointerControllers currently needed, and create |
| 435 | // new PointerControllers if necessary. |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 436 | for (const auto& info : mInputDeviceInfos) { |
| 437 | const uint32_t sources = info.getSources(); |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 438 | const bool isKnownMouse = mMouseDevices.count(info.getId()) != 0; |
| 439 | |
| 440 | if (isMouseOrTouchpad(sources) || isKnownMouse) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 441 | const int32_t displayId = getTargetMouseDisplayLocked(info.getAssociatedDisplayId()); |
| 442 | mouseDisplaysToKeep.insert(displayId); |
| 443 | // For mice, show the cursor immediately when the device is first connected or |
| 444 | // when it moves to a new display. |
| 445 | auto [mousePointerIt, isNewMousePointer] = |
| 446 | mMousePointersByDisplay.try_emplace(displayId, |
| 447 | getMouseControllerConstructor(displayId)); |
Prabir Pradhan | 5a31d3c | 2024-03-29 20:23:22 +0000 | [diff] [blame] | 448 | mMouseDevices.emplace(info.getId()); |
| 449 | if ((!isKnownMouse || isNewMousePointer) && canUnfadeOnDisplay(displayId)) { |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 450 | mousePointerIt->second->unfade(PointerControllerInterface::Transition::IMMEDIATE); |
| 451 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 452 | } |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 453 | if (isFromSource(sources, AINPUT_SOURCE_TOUCHSCREEN) && mShowTouchesEnabled && |
| 454 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 455 | touchDevicesToKeep.insert(info.getId()); |
| 456 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 457 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS) && mStylusPointerIconEnabled && |
| 458 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 459 | stylusDevicesToKeep.insert(info.getId()); |
| 460 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 461 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE) && |
| 462 | info.getAssociatedDisplayId() != ADISPLAY_ID_NONE) { |
| 463 | drawingTabletDevicesToKeep.insert(info.getId()); |
| 464 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // Remove PointerControllers no longer needed. |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 468 | std::erase_if(mMousePointersByDisplay, [&mouseDisplaysToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 469 | return mouseDisplaysToKeep.find(pair.first) == mouseDisplaysToKeep.end(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 470 | }); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 471 | std::erase_if(mTouchPointersByDevice, [&touchDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 472 | return touchDevicesToKeep.find(pair.first) == touchDevicesToKeep.end(); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 473 | }); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 474 | std::erase_if(mStylusPointersByDevice, [&stylusDevicesToKeep](const auto& pair) { |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 475 | return stylusDevicesToKeep.find(pair.first) == stylusDevicesToKeep.end(); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 476 | }); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 477 | std::erase_if(mDrawingTabletPointersByDevice, [&drawingTabletDevicesToKeep](const auto& pair) { |
| 478 | return drawingTabletDevicesToKeep.find(pair.first) == drawingTabletDevicesToKeep.end(); |
| 479 | }); |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 480 | std::erase_if(mMouseDevices, [&](DeviceId id) REQUIRES(mLock) { |
| 481 | return std::find_if(mInputDeviceInfos.begin(), mInputDeviceInfos.end(), |
| 482 | [id](const auto& info) { return info.getId() == id; }) == |
| 483 | mInputDeviceInfos.end(); |
| 484 | }); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 485 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 486 | // Check if we need to notify the policy if there's a change on the pointer display ID. |
| 487 | return calculatePointerDisplayChangeToNotify(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 488 | } |
| 489 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 490 | PointerChoreographer::PointerDisplayChange |
| 491 | PointerChoreographer::calculatePointerDisplayChangeToNotify() { |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 492 | int32_t displayIdToNotify = ADISPLAY_ID_NONE; |
| 493 | FloatPoint cursorPosition = {0, 0}; |
| 494 | if (const auto it = mMousePointersByDisplay.find(mDefaultMouseDisplayId); |
| 495 | it != mMousePointersByDisplay.end()) { |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 496 | const auto& pointerController = it->second; |
| 497 | // Use the displayId from the pointerController, because it accurately reflects whether |
| 498 | // the viewport has been added for that display. Otherwise, we would have to check if |
| 499 | // the viewport exists separately. |
| 500 | displayIdToNotify = pointerController->getDisplayId(); |
| 501 | cursorPosition = pointerController->getPosition(); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 502 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 503 | if (mNotifiedPointerDisplayId == displayIdToNotify) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 504 | return {}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 505 | } |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 506 | mNotifiedPointerDisplayId = displayIdToNotify; |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 507 | return {{displayIdToNotify, cursorPosition}}; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void PointerChoreographer::setDefaultMouseDisplayId(int32_t displayId) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 511 | PointerDisplayChange pointerDisplayChange; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 512 | |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 513 | { // acquire lock |
| 514 | std::scoped_lock _l(mLock); |
| 515 | |
| 516 | mDefaultMouseDisplayId = displayId; |
| 517 | pointerDisplayChange = updatePointerControllersLocked(); |
| 518 | } // release lock |
| 519 | |
| 520 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void PointerChoreographer::setDisplayViewports(const std::vector<DisplayViewport>& viewports) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 524 | PointerDisplayChange pointerDisplayChange; |
| 525 | |
| 526 | { // acquire lock |
| 527 | std::scoped_lock _l(mLock); |
| 528 | for (const auto& viewport : viewports) { |
| 529 | const int32_t displayId = viewport.displayId; |
| 530 | if (const auto it = mMousePointersByDisplay.find(displayId); |
| 531 | it != mMousePointersByDisplay.end()) { |
| 532 | it->second->setDisplayViewport(viewport); |
| 533 | } |
| 534 | for (const auto& [deviceId, stylusPointerController] : mStylusPointersByDevice) { |
| 535 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 536 | if (info && info->getAssociatedDisplayId() == displayId) { |
| 537 | stylusPointerController->setDisplayViewport(viewport); |
| 538 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 539 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 540 | for (const auto& [deviceId, drawingTabletController] : mDrawingTabletPointersByDevice) { |
| 541 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 542 | if (info && info->getAssociatedDisplayId() == displayId) { |
| 543 | drawingTabletController->setDisplayViewport(viewport); |
| 544 | } |
| 545 | } |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 546 | } |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 547 | mViewports = viewports; |
| 548 | pointerDisplayChange = calculatePointerDisplayChangeToNotify(); |
| 549 | } // release lock |
| 550 | |
| 551 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | std::optional<DisplayViewport> PointerChoreographer::getViewportForPointerDevice( |
| 555 | int32_t associatedDisplayId) { |
| 556 | std::scoped_lock _l(mLock); |
| 557 | const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(associatedDisplayId); |
| 558 | if (const auto viewport = findViewportByIdLocked(resolvedDisplayId); viewport) { |
| 559 | return *viewport; |
| 560 | } |
| 561 | return std::nullopt; |
| 562 | } |
| 563 | |
| 564 | FloatPoint PointerChoreographer::getMouseCursorPosition(int32_t displayId) { |
| 565 | std::scoped_lock _l(mLock); |
| 566 | const int32_t resolvedDisplayId = getTargetMouseDisplayLocked(displayId); |
| 567 | if (auto it = mMousePointersByDisplay.find(resolvedDisplayId); |
| 568 | it != mMousePointersByDisplay.end()) { |
| 569 | return it->second->getPosition(); |
| 570 | } |
| 571 | return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 574 | void PointerChoreographer::setShowTouchesEnabled(bool enabled) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 575 | PointerDisplayChange pointerDisplayChange; |
| 576 | |
| 577 | { // acquire lock |
| 578 | std::scoped_lock _l(mLock); |
| 579 | if (mShowTouchesEnabled == enabled) { |
| 580 | return; |
| 581 | } |
| 582 | mShowTouchesEnabled = enabled; |
| 583 | pointerDisplayChange = updatePointerControllersLocked(); |
| 584 | } // release lock |
| 585 | |
| 586 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 587 | } |
| 588 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 589 | void PointerChoreographer::setStylusPointerIconEnabled(bool enabled) { |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 590 | PointerDisplayChange pointerDisplayChange; |
| 591 | |
| 592 | { // acquire lock |
| 593 | std::scoped_lock _l(mLock); |
| 594 | if (mStylusPointerIconEnabled == enabled) { |
| 595 | return; |
| 596 | } |
| 597 | mStylusPointerIconEnabled = enabled; |
| 598 | pointerDisplayChange = updatePointerControllersLocked(); |
| 599 | } // release lock |
| 600 | |
| 601 | notifyPointerDisplayChange(pointerDisplayChange, mPolicy); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 602 | } |
| 603 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 604 | bool PointerChoreographer::setPointerIcon( |
| 605 | std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, int32_t displayId, |
| 606 | DeviceId deviceId) { |
| 607 | std::scoped_lock _l(mLock); |
| 608 | if (deviceId < 0) { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 609 | LOG(WARNING) << "Invalid device id " << deviceId << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 610 | return false; |
| 611 | } |
| 612 | const InputDeviceInfo* info = findInputDeviceLocked(deviceId); |
| 613 | if (!info) { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 614 | LOG(WARNING) << "No input device info found for id " << deviceId |
| 615 | << ". Cannot set pointer icon."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 616 | return false; |
| 617 | } |
| 618 | const uint32_t sources = info->getSources(); |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 619 | |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 620 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_MOUSE)) { |
| 621 | auto it = mDrawingTabletPointersByDevice.find(deviceId); |
| 622 | if (it != mDrawingTabletPointersByDevice.end()) { |
| 623 | setIconForController(icon, *it->second); |
| 624 | return true; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 625 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 626 | } |
| 627 | if (isFromSource(sources, AINPUT_SOURCE_STYLUS)) { |
| 628 | auto it = mStylusPointersByDevice.find(deviceId); |
| 629 | if (it != mStylusPointersByDevice.end()) { |
| 630 | setIconForController(icon, *it->second); |
| 631 | return true; |
| 632 | } |
| 633 | } |
| 634 | if (isFromSource(sources, AINPUT_SOURCE_MOUSE)) { |
| 635 | auto it = mMousePointersByDisplay.find(displayId); |
| 636 | if (it != mMousePointersByDisplay.end()) { |
| 637 | setIconForController(icon, *it->second); |
| 638 | return true; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 639 | } else { |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 640 | LOG(WARNING) << "No mouse pointer controller found for display " << displayId |
| 641 | << ", device " << deviceId << "."; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 642 | return false; |
| 643 | } |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 644 | } |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 645 | LOG(WARNING) << "Cannot set pointer icon for display " << displayId << ", device " << deviceId |
| 646 | << "."; |
| 647 | return false; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 648 | } |
| 649 | |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 650 | void PointerChoreographer::setPointerIconVisibility(int32_t displayId, bool visible) { |
| 651 | std::scoped_lock lock(mLock); |
| 652 | if (visible) { |
| 653 | mDisplaysWithPointersHidden.erase(displayId); |
| 654 | // We do not unfade the icons here, because we don't know when the last event happened. |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | mDisplaysWithPointersHidden.emplace(displayId); |
| 659 | |
| 660 | // Hide any icons that are currently visible on the display. |
| 661 | if (auto it = mMousePointersByDisplay.find(displayId); it != mMousePointersByDisplay.end()) { |
| 662 | const auto& [_, controller] = *it; |
| 663 | controller->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 664 | } |
| 665 | for (const auto& [_, controller] : mStylusPointersByDevice) { |
| 666 | if (controller->getDisplayId() == displayId) { |
| 667 | controller->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 672 | PointerChoreographer::ControllerConstructor PointerChoreographer::getMouseControllerConstructor( |
| 673 | int32_t displayId) { |
| 674 | std::function<std::shared_ptr<PointerControllerInterface>()> ctor = |
| 675 | [this, displayId]() REQUIRES(mLock) { |
| 676 | auto pc = mPolicy.createPointerController( |
| 677 | PointerControllerInterface::ControllerType::MOUSE); |
| 678 | if (const auto viewport = findViewportByIdLocked(displayId); viewport) { |
| 679 | pc->setDisplayViewport(*viewport); |
| 680 | } |
| 681 | return pc; |
| 682 | }; |
| 683 | return ConstructorDelegate(std::move(ctor)); |
| 684 | } |
| 685 | |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 686 | PointerChoreographer::ControllerConstructor PointerChoreographer::getStylusControllerConstructor( |
| 687 | int32_t displayId) { |
| 688 | std::function<std::shared_ptr<PointerControllerInterface>()> ctor = |
| 689 | [this, displayId]() REQUIRES(mLock) { |
| 690 | auto pc = mPolicy.createPointerController( |
| 691 | PointerControllerInterface::ControllerType::STYLUS); |
| 692 | if (const auto viewport = findViewportByIdLocked(displayId); viewport) { |
| 693 | pc->setDisplayViewport(*viewport); |
| 694 | } |
| 695 | return pc; |
| 696 | }; |
| 697 | return ConstructorDelegate(std::move(ctor)); |
| 698 | } |
| 699 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 700 | } // namespace android |