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> |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 24 | #include <type_traits> |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 25 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame^] | 28 | struct SpriteIcon; |
| 29 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 30 | /** |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 31 | * A helper class that wraps a factory method that acts as a constructor for the type returned |
| 32 | * by the factory method. |
| 33 | */ |
| 34 | template <typename Factory> |
| 35 | struct ConstructorDelegate { |
| 36 | constexpr ConstructorDelegate(Factory&& factory) : mFactory(std::move(factory)) {} |
| 37 | |
| 38 | using ConstructedType = std::invoke_result_t<const Factory&>; |
| 39 | constexpr operator ConstructedType() const { return mFactory(); } |
| 40 | |
| 41 | Factory mFactory; |
| 42 | }; |
| 43 | |
| 44 | /** |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 45 | * PointerChoreographer manages the icons shown by the system for input interactions. |
| 46 | * This includes showing the mouse cursor, stylus hover icons, and touch spots. |
| 47 | * It is responsible for accumulating the location of the mouse cursor, and populating |
| 48 | * the cursor position for incoming events, if necessary. |
| 49 | */ |
| 50 | class PointerChoreographerInterface : public InputListenerInterface { |
| 51 | public: |
| 52 | /** |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 53 | * Set the display that pointers, like the mouse cursor and drawing tablets, |
| 54 | * should be drawn on. |
| 55 | */ |
| 56 | virtual void setDefaultMouseDisplayId(int32_t displayId) = 0; |
| 57 | virtual void setDisplayViewports(const std::vector<DisplayViewport>& viewports) = 0; |
| 58 | virtual std::optional<DisplayViewport> getViewportForPointerDevice( |
| 59 | int32_t associatedDisplayId = ADISPLAY_ID_NONE) = 0; |
| 60 | virtual FloatPoint getMouseCursorPosition(int32_t displayId) = 0; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 61 | virtual void setShowTouchesEnabled(bool enabled) = 0; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 62 | virtual void setStylusPointerIconEnabled(bool enabled) = 0; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame^] | 63 | virtual bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, |
| 64 | int32_t displayId, DeviceId deviceId) = 0; |
| 65 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 66 | /** |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 67 | * This method may be called on any thread (usually by the input manager on a binder thread). |
| 68 | */ |
| 69 | virtual void dump(std::string& dump) = 0; |
| 70 | }; |
| 71 | |
| 72 | class PointerChoreographer : public PointerChoreographerInterface { |
| 73 | public: |
| 74 | explicit PointerChoreographer(InputListenerInterface& listener, |
| 75 | PointerChoreographerPolicyInterface&); |
| 76 | ~PointerChoreographer() override = default; |
| 77 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 78 | void setDefaultMouseDisplayId(int32_t displayId) override; |
| 79 | void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override; |
| 80 | std::optional<DisplayViewport> getViewportForPointerDevice( |
| 81 | int32_t associatedDisplayId) override; |
| 82 | FloatPoint getMouseCursorPosition(int32_t displayId) override; |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 83 | void setShowTouchesEnabled(bool enabled) override; |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 84 | void setStylusPointerIconEnabled(bool enabled) override; |
Byoungho Jung | 9932645 | 2023-11-03 20:19:17 +0900 | [diff] [blame^] | 85 | bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, |
| 86 | int32_t displayId, DeviceId deviceId) override; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 87 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 88 | void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; |
| 89 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override; |
| 90 | void notifyKey(const NotifyKeyArgs& args) override; |
| 91 | void notifyMotion(const NotifyMotionArgs& args) override; |
| 92 | void notifySwitch(const NotifySwitchArgs& args) override; |
| 93 | void notifySensor(const NotifySensorArgs& args) override; |
| 94 | void notifyVibratorState(const NotifyVibratorStateArgs& args) override; |
| 95 | void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; |
| 96 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; |
| 97 | |
| 98 | void dump(std::string& dump) override; |
| 99 | |
| 100 | private: |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 101 | void updatePointerControllersLocked() REQUIRES(mLock); |
| 102 | void notifyPointerDisplayIdChangedLocked() REQUIRES(mLock); |
| 103 | const DisplayViewport* findViewportByIdLocked(int32_t displayId) const REQUIRES(mLock); |
| 104 | int32_t getTargetMouseDisplayLocked(int32_t associatedDisplayId) const REQUIRES(mLock); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 105 | std::pair<int32_t, PointerControllerInterface&> getDisplayIdAndMouseControllerLocked( |
| 106 | int32_t associatedDisplayId) REQUIRES(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 107 | InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 108 | |
| 109 | NotifyMotionArgs processMotion(const NotifyMotionArgs& args); |
| 110 | NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 111 | NotifyMotionArgs processTouchpadEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 112 | void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 113 | void processStylusHoverEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 114 | void processDeviceReset(const NotifyDeviceResetArgs& args); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 115 | |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 116 | using ControllerConstructor = |
| 117 | ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>; |
Prabir Pradhan | 1678879 | 2023-11-08 21:07:21 +0000 | [diff] [blame] | 118 | ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock); |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 119 | ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 120 | ControllerConstructor getStylusControllerConstructor(int32_t displayId) REQUIRES(mLock); |
Prabir Pradhan | 1976760 | 2023-11-03 16:53:31 +0000 | [diff] [blame] | 121 | |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 122 | std::mutex mLock; |
| 123 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 124 | InputListenerInterface& mNextListener; |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 125 | PointerChoreographerPolicyInterface& mPolicy; |
| 126 | |
| 127 | std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay |
| 128 | GUARDED_BY(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 129 | std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice |
| 130 | GUARDED_BY(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 131 | std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice |
| 132 | GUARDED_BY(mLock); |
Byoungho Jung | da10dd3 | 2023-10-06 17:03:45 +0900 | [diff] [blame] | 133 | |
| 134 | int32_t mDefaultMouseDisplayId GUARDED_BY(mLock); |
| 135 | int32_t mNotifiedPointerDisplayId GUARDED_BY(mLock); |
| 136 | std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock); |
| 137 | std::vector<DisplayViewport> mViewports GUARDED_BY(mLock); |
Byoungho Jung | 6f5b16b | 2023-10-27 18:22:07 +0900 | [diff] [blame] | 138 | bool mShowTouchesEnabled GUARDED_BY(mLock); |
Byoungho Jung | d6fe27b | 2023-10-27 20:49:38 +0900 | [diff] [blame] | 139 | bool mStylusPointerIconEnabled GUARDED_BY(mLock); |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } // namespace android |