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