blob: c1b900f8a58e04cf8a2d913b3e2d94245f3a7a2e [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;
59 /**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000060 * This method may be called on any thread (usually by the input manager on a binder thread).
61 */
62 virtual void dump(std::string& dump) = 0;
63};
64
65class PointerChoreographer : public PointerChoreographerInterface {
66public:
67 explicit PointerChoreographer(InputListenerInterface& listener,
68 PointerChoreographerPolicyInterface&);
69 ~PointerChoreographer() override = default;
70
Byoungho Jungda10dd32023-10-06 17:03:45 +090071 void setDefaultMouseDisplayId(int32_t displayId) override;
72 void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override;
73 std::optional<DisplayViewport> getViewportForPointerDevice(
74 int32_t associatedDisplayId) override;
75 FloatPoint getMouseCursorPosition(int32_t displayId) override;
76
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000077 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
78 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
79 void notifyKey(const NotifyKeyArgs& args) override;
80 void notifyMotion(const NotifyMotionArgs& args) override;
81 void notifySwitch(const NotifySwitchArgs& args) override;
82 void notifySensor(const NotifySensorArgs& args) override;
83 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
84 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
85 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
86
87 void dump(std::string& dump) override;
88
89private:
Byoungho Jungda10dd32023-10-06 17:03:45 +090090 void updatePointerControllersLocked() REQUIRES(mLock);
91 void notifyPointerDisplayIdChangedLocked() REQUIRES(mLock);
92 const DisplayViewport* findViewportByIdLocked(int32_t displayId) const REQUIRES(mLock);
93 int32_t getTargetMouseDisplayLocked(int32_t associatedDisplayId) const REQUIRES(mLock);
94
95 NotifyMotionArgs processMotion(const NotifyMotionArgs& args);
96 NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
97 NotifyMotionArgs processTouchscreenEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
98
Prabir Pradhan19767602023-11-03 16:53:31 +000099 using ControllerConstructor =
100 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
101 ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock);
102
Byoungho Jungda10dd32023-10-06 17:03:45 +0900103 std::mutex mLock;
104
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000105 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900106 PointerChoreographerPolicyInterface& mPolicy;
107
108 std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay
109 GUARDED_BY(mLock);
110
111 int32_t mDefaultMouseDisplayId GUARDED_BY(mLock);
112 int32_t mNotifiedPointerDisplayId GUARDED_BY(mLock);
113 std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock);
114 std::vector<DisplayViewport> mViewports GUARDED_BY(mLock);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000115};
116
117} // namespace android