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 | #pragma once |
| 18 | |
| 19 | #include "InputListener.h" |
| 20 | #include "NotifyArgs.h" |
| 21 | #include "PointerChoreographerPolicyInterface.h" |
| 22 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 24 | #include <gui/WindowInfosListener.h> |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 25 | #include <type_traits> |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 26 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 29 | struct SpriteIcon; |
| 30 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 31 | /** |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 32 | * A helper class that wraps a factory method that acts as a constructor for the type returned |
| 33 | * by the factory method. |
| 34 | */ |
| 35 | template <typename Factory> |
| 36 | struct ConstructorDelegate { |
| 37 | constexpr ConstructorDelegate(Factory&& factory) : mFactory(std::move(factory)) {} |
| 38 | |
| 39 | using ConstructedType = std::invoke_result_t<const Factory&>; |
| 40 | constexpr operator ConstructedType() const { return mFactory(); } |
| 41 | |
| 42 | Factory mFactory; |
| 43 | }; |
| 44 | |
| 45 | /** |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 46 | * PointerChoreographer manages the icons shown by the system for input interactions. |
| 47 | * This includes showing the mouse cursor, stylus hover icons, and touch spots. |
| 48 | * It is responsible for accumulating the location of the mouse cursor, and populating |
| 49 | * the cursor position for incoming events, if necessary. |
| 50 | */ |
| 51 | class PointerChoreographerInterface : public InputListenerInterface { |
| 52 | public: |
| 53 | /** |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 54 | * Set the display that pointers, like the mouse cursor and drawing tablets, |
| 55 | * should be drawn on. |
| 56 | */ |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 57 | virtual void setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) = 0; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 58 | virtual void setDisplayViewports(const std::vector<DisplayViewport>& viewports) = 0; |
| 59 | virtual std::optional<DisplayViewport> getViewportForPointerDevice( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 60 | ui::LogicalDisplayId associatedDisplayId = ui::ADISPLAY_ID_NONE) = 0; |
| 61 | virtual FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId) = 0; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 62 | virtual void setShowTouchesEnabled(bool enabled) = 0; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 63 | virtual void setStylusPointerIconEnabled(bool enabled) = 0; |
Prabir Pradhan | 521f4fc | 2023-12-04 19:09:59 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Set the icon that is shown for the given pointer. The request may fail in some cases, such |
| 66 | * as if the device or display was removed, or if the cursor was moved to a different display. |
| 67 | * Returns true if the icon was changed successfully, false otherwise. |
| 68 | */ |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 69 | virtual bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 70 | ui::LogicalDisplayId displayId, DeviceId deviceId) = 0; |
Prabir Pradhan | 502ddbd | 2024-01-19 02:22:38 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Set whether pointer icons for mice, touchpads, and styluses should be visible on the |
| 73 | * given display. |
| 74 | */ |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 75 | virtual void setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) = 0; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 76 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 77 | /** |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 78 | * This method may be called on any thread (usually by the input manager on a binder thread). |
| 79 | */ |
| 80 | virtual void dump(std::string& dump) = 0; |
| 81 | }; |
| 82 | |
| 83 | class PointerChoreographer : public PointerChoreographerInterface { |
| 84 | public: |
| 85 | explicit PointerChoreographer(InputListenerInterface& listener, |
| 86 | PointerChoreographerPolicyInterface&); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 87 | ~PointerChoreographer() override; |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 88 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 89 | void setDefaultMouseDisplayId(ui::LogicalDisplayId displayId) override; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 90 | void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override; |
| 91 | std::optional<DisplayViewport> getViewportForPointerDevice( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 92 | ui::LogicalDisplayId associatedDisplayId) override; |
| 93 | FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId) override; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 94 | void setShowTouchesEnabled(bool enabled) override; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 95 | void setStylusPointerIconEnabled(bool enabled) override; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame] | 96 | bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 97 | ui::LogicalDisplayId displayId, DeviceId deviceId) override; |
| 98 | void setPointerIconVisibility(ui::LogicalDisplayId displayId, bool visible) override; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 99 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 100 | void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; |
| 101 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override; |
| 102 | void notifyKey(const NotifyKeyArgs& args) override; |
| 103 | void notifyMotion(const NotifyMotionArgs& args) override; |
| 104 | void notifySwitch(const NotifySwitchArgs& args) override; |
| 105 | void notifySensor(const NotifySensorArgs& args) override; |
| 106 | void notifyVibratorState(const NotifyVibratorStateArgs& args) override; |
| 107 | void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; |
| 108 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; |
| 109 | |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 110 | // Public because it's also used by tests to simulate the WindowInfosListener callback |
| 111 | void onWindowInfosChanged(const std::vector<android::gui::WindowInfo>& windowInfos); |
| 112 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 113 | void dump(std::string& dump) override; |
| 114 | |
| 115 | private: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 116 | using PointerDisplayChange = std::optional< |
| 117 | std::tuple<ui::LogicalDisplayId /*displayId*/, FloatPoint /*cursorPosition*/>>; |
Prabir Pradhan | 5a51a22 | 2024-03-05 03:54:00 +0000 | [diff] [blame] | 118 | [[nodiscard]] PointerDisplayChange updatePointerControllersLocked() REQUIRES(mLock); |
| 119 | [[nodiscard]] PointerDisplayChange calculatePointerDisplayChangeToNotify() REQUIRES(mLock); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 120 | const DisplayViewport* findViewportByIdLocked(ui::LogicalDisplayId displayId) const |
| 121 | REQUIRES(mLock); |
| 122 | ui::LogicalDisplayId getTargetMouseDisplayLocked(ui::LogicalDisplayId associatedDisplayId) const |
| 123 | REQUIRES(mLock); |
| 124 | std::pair<ui::LogicalDisplayId /*displayId*/, PointerControllerInterface&> |
| 125 | ensureMouseControllerLocked(ui::LogicalDisplayId associatedDisplayId) REQUIRES(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 126 | InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 127 | bool canUnfadeOnDisplay(ui::LogicalDisplayId displayId) REQUIRES(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 128 | |
| 129 | NotifyMotionArgs processMotion(const NotifyMotionArgs& args); |
| 130 | NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 131 | NotifyMotionArgs processTouchpadEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 132 | void processDrawingTabletEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 133 | void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 134 | void processStylusHoverEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 135 | void processDeviceReset(const NotifyDeviceResetArgs& args); |
Arpit Singh | 4b6ad2d | 2024-04-04 11:54:20 +0000 | [diff] [blame] | 136 | void onControllerAddedOrRemoved() REQUIRES(mLock); |
| 137 | void onWindowInfosChangedLocked(const std::vector<android::gui::WindowInfo>& windowInfos) |
| 138 | REQUIRES(mLock); |
| 139 | |
| 140 | class PointerChoreographerDisplayInfoListener : public gui::WindowInfosListener { |
| 141 | public: |
| 142 | explicit PointerChoreographerDisplayInfoListener(PointerChoreographer* pc) |
| 143 | : mPointerChoreographer(pc){}; |
| 144 | void onWindowInfosChanged(const gui::WindowInfosUpdate&) override; |
| 145 | void onPointerChoreographerDestroyed(); |
| 146 | |
| 147 | private: |
| 148 | std::mutex mListenerLock; |
| 149 | PointerChoreographer* mPointerChoreographer GUARDED_BY(mListenerLock); |
| 150 | }; |
| 151 | sp<PointerChoreographerDisplayInfoListener> mWindowInfoListener GUARDED_BY(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 152 | |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 153 | using ControllerConstructor = |
| 154 | ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>; |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 155 | ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 156 | ControllerConstructor getMouseControllerConstructor(ui::LogicalDisplayId displayId) |
| 157 | REQUIRES(mLock); |
| 158 | ControllerConstructor getStylusControllerConstructor(ui::LogicalDisplayId displayId) |
| 159 | REQUIRES(mLock); |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 160 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 161 | std::mutex mLock; |
| 162 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 163 | InputListenerInterface& mNextListener; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 164 | PointerChoreographerPolicyInterface& mPolicy; |
| 165 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 166 | std::map<ui::LogicalDisplayId, std::shared_ptr<PointerControllerInterface>> |
| 167 | mMousePointersByDisplay GUARDED_BY(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 168 | std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice |
| 169 | GUARDED_BY(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 170 | std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice |
| 171 | GUARDED_BY(mLock); |
Prabir Pradhan | 4c977a4 | 2024-03-15 16:47:37 +0000 | [diff] [blame] | 172 | std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mDrawingTabletPointersByDevice |
| 173 | GUARDED_BY(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 174 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 175 | ui::LogicalDisplayId mDefaultMouseDisplayId GUARDED_BY(mLock); |
| 176 | ui::LogicalDisplayId mNotifiedPointerDisplayId GUARDED_BY(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 177 | std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock); |
Prabir Pradhan | 6506f6f | 2023-12-11 20:48:39 +0000 | [diff] [blame] | 178 | std::set<DeviceId> mMouseDevices GUARDED_BY(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 179 | std::vector<DisplayViewport> mViewports GUARDED_BY(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 180 | bool mShowTouchesEnabled GUARDED_BY(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 181 | bool mStylusPointerIconEnabled GUARDED_BY(mLock); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 182 | std::set<ui::LogicalDisplayId /*displayId*/> mDisplaysWithPointersHidden; |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // namespace android |