blob: 84eb1e7c47743383219adb9171cfe56727ca30cc [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
28/**
Prabir Pradhan19767602023-11-03 16:53:31 +000029 * A helper class that wraps a factory method that acts as a constructor for the type returned
30 * by the factory method.
31 */
32template <typename Factory>
33struct 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 Pradhanb56e92c2023-06-09 23:40:37 +000043 * 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 */
48class PointerChoreographerInterface : public InputListenerInterface {
49public:
50 /**
Byoungho Jungda10dd32023-10-06 17:03:45 +090051 * 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 Jung6f5b16b2023-10-27 18:22:07 +090059 virtual void setShowTouchesEnabled(bool enabled) = 0;
Byoungho Jungda10dd32023-10-06 17:03:45 +090060 /**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000061 * 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
66class PointerChoreographer : public PointerChoreographerInterface {
67public:
68 explicit PointerChoreographer(InputListenerInterface& listener,
69 PointerChoreographerPolicyInterface&);
70 ~PointerChoreographer() override = default;
71
Byoungho Jungda10dd32023-10-06 17:03:45 +090072 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 Jung6f5b16b2023-10-27 18:22:07 +090077 void setShowTouchesEnabled(bool enabled) override;
Byoungho Jungda10dd32023-10-06 17:03:45 +090078
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000079 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
91private:
Byoungho Jungda10dd32023-10-06 17:03:45 +090092 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 Jung6f5b16b2023-10-27 18:22:07 +090096 InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +090097
98 NotifyMotionArgs processMotion(const NotifyMotionArgs& args);
99 NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900100 void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
101 void processDeviceReset(const NotifyDeviceResetArgs& args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900102
Prabir Pradhan19767602023-11-03 16:53:31 +0000103 using ControllerConstructor =
104 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
105 ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900106 ControllerConstructor getTouchControllerConstructor() REQUIRES(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000107
Byoungho Jungda10dd32023-10-06 17:03:45 +0900108 std::mutex mLock;
109
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000110 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900111 PointerChoreographerPolicyInterface& mPolicy;
112
113 std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay
114 GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900115 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice
116 GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900117
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 Jung6f5b16b2023-10-27 18:22:07 +0900122 bool mShowTouchesEnabled GUARDED_BY(mLock);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000123};
124
125} // namespace android