blob: f46419ec2eb96227df82f86b7f5ef5b92a5982de [file] [log] [blame]
Prabir Pradhanb56e92c2023-06-09 23:40:37 +00001/*
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 Jungda10dd32023-10-06 17:03:45 +090023#include <android-base/thread_annotations.h>
Prabir Pradhan19767602023-11-03 16:53:31 +000024#include <type_traits>
Byoungho Jungda10dd32023-10-06 17:03:45 +090025
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000026namespace android {
27
Byoungho Jung99326452023-11-03 20:19:17 +090028struct SpriteIcon;
29
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000030/**
Prabir Pradhan19767602023-11-03 16:53:31 +000031 * A helper class that wraps a factory method that acts as a constructor for the type returned
32 * by the factory method.
33 */
34template <typename Factory>
35struct 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 Pradhanb56e92c2023-06-09 23:40:37 +000045 * 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 */
50class PointerChoreographerInterface : public InputListenerInterface {
51public:
52 /**
Byoungho Jungda10dd32023-10-06 17:03:45 +090053 * 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 Jung6f5b16b2023-10-27 18:22:07 +090061 virtual void setShowTouchesEnabled(bool enabled) = 0;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090062 virtual void setStylusPointerIconEnabled(bool enabled) = 0;
Prabir Pradhan521f4fc2023-12-04 19:09:59 +000063 /**
64 * Set the icon that is shown for the given pointer. The request may fail in some cases, such
65 * as if the device or display was removed, or if the cursor was moved to a different display.
66 * Returns true if the icon was changed successfully, false otherwise.
67 */
Byoungho Jung99326452023-11-03 20:19:17 +090068 virtual bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
69 int32_t displayId, DeviceId deviceId) = 0;
70
Byoungho Jungda10dd32023-10-06 17:03:45 +090071 /**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000072 * This method may be called on any thread (usually by the input manager on a binder thread).
73 */
74 virtual void dump(std::string& dump) = 0;
75};
76
77class PointerChoreographer : public PointerChoreographerInterface {
78public:
79 explicit PointerChoreographer(InputListenerInterface& listener,
80 PointerChoreographerPolicyInterface&);
81 ~PointerChoreographer() override = default;
82
Byoungho Jungda10dd32023-10-06 17:03:45 +090083 void setDefaultMouseDisplayId(int32_t displayId) override;
84 void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override;
85 std::optional<DisplayViewport> getViewportForPointerDevice(
86 int32_t associatedDisplayId) override;
87 FloatPoint getMouseCursorPosition(int32_t displayId) override;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090088 void setShowTouchesEnabled(bool enabled) override;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090089 void setStylusPointerIconEnabled(bool enabled) override;
Byoungho Jung99326452023-11-03 20:19:17 +090090 bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
91 int32_t displayId, DeviceId deviceId) override;
Byoungho Jungda10dd32023-10-06 17:03:45 +090092
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000093 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
94 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
95 void notifyKey(const NotifyKeyArgs& args) override;
96 void notifyMotion(const NotifyMotionArgs& args) override;
97 void notifySwitch(const NotifySwitchArgs& args) override;
98 void notifySensor(const NotifySensorArgs& args) override;
99 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
100 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
101 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
102
103 void dump(std::string& dump) override;
104
105private:
Byoungho Jungda10dd32023-10-06 17:03:45 +0900106 void updatePointerControllersLocked() REQUIRES(mLock);
107 void notifyPointerDisplayIdChangedLocked() REQUIRES(mLock);
108 const DisplayViewport* findViewportByIdLocked(int32_t displayId) const REQUIRES(mLock);
109 int32_t getTargetMouseDisplayLocked(int32_t associatedDisplayId) const REQUIRES(mLock);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900110 std::pair<int32_t, PointerControllerInterface&> getDisplayIdAndMouseControllerLocked(
111 int32_t associatedDisplayId) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900112 InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900113
114 NotifyMotionArgs processMotion(const NotifyMotionArgs& args);
115 NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900116 NotifyMotionArgs processTouchpadEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900117 void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900118 void processStylusHoverEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900119 void processDeviceReset(const NotifyDeviceResetArgs& args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900120
Prabir Pradhan19767602023-11-03 16:53:31 +0000121 using ControllerConstructor =
122 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
Prabir Pradhan16788792023-11-08 21:07:21 +0000123 ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000124 ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900125 ControllerConstructor getStylusControllerConstructor(int32_t displayId) REQUIRES(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000126
Byoungho Jungda10dd32023-10-06 17:03:45 +0900127 std::mutex mLock;
128
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000129 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900130 PointerChoreographerPolicyInterface& mPolicy;
131
132 std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay
133 GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900134 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice
135 GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900136 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice
137 GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900138
139 int32_t mDefaultMouseDisplayId GUARDED_BY(mLock);
140 int32_t mNotifiedPointerDisplayId GUARDED_BY(mLock);
141 std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock);
Prabir Pradhan6506f6f2023-12-11 20:48:39 +0000142 std::set<DeviceId> mMouseDevices GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900143 std::vector<DisplayViewport> mViewports GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900144 bool mShowTouchesEnabled GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900145 bool mStylusPointerIconEnabled GUARDED_BY(mLock);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000146};
147
148} // namespace android