blob: 90a0d3fc72df17c46f71a0b991ec31e998b7bccd [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 Jungd6fe27b2023-10-27 20:49:38 +090060 virtual void setStylusPointerIconEnabled(bool enabled) = 0;
Byoungho Jungda10dd32023-10-06 17:03:45 +090061 /**
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000062 * This method may be called on any thread (usually by the input manager on a binder thread).
63 */
64 virtual void dump(std::string& dump) = 0;
65};
66
67class PointerChoreographer : public PointerChoreographerInterface {
68public:
69 explicit PointerChoreographer(InputListenerInterface& listener,
70 PointerChoreographerPolicyInterface&);
71 ~PointerChoreographer() override = default;
72
Byoungho Jungda10dd32023-10-06 17:03:45 +090073 void setDefaultMouseDisplayId(int32_t displayId) override;
74 void setDisplayViewports(const std::vector<DisplayViewport>& viewports) override;
75 std::optional<DisplayViewport> getViewportForPointerDevice(
76 int32_t associatedDisplayId) override;
77 FloatPoint getMouseCursorPosition(int32_t displayId) override;
Byoungho Jung6f5b16b2023-10-27 18:22:07 +090078 void setShowTouchesEnabled(bool enabled) override;
Byoungho Jungd6fe27b2023-10-27 20:49:38 +090079 void setStylusPointerIconEnabled(bool enabled) override;
Byoungho Jungda10dd32023-10-06 17:03:45 +090080
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000081 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
82 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
83 void notifyKey(const NotifyKeyArgs& args) override;
84 void notifyMotion(const NotifyMotionArgs& args) override;
85 void notifySwitch(const NotifySwitchArgs& args) override;
86 void notifySensor(const NotifySensorArgs& args) override;
87 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
88 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
89 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
90
91 void dump(std::string& dump) override;
92
93private:
Byoungho Jungda10dd32023-10-06 17:03:45 +090094 void updatePointerControllersLocked() REQUIRES(mLock);
95 void notifyPointerDisplayIdChangedLocked() REQUIRES(mLock);
96 const DisplayViewport* findViewportByIdLocked(int32_t displayId) const REQUIRES(mLock);
97 int32_t getTargetMouseDisplayLocked(int32_t associatedDisplayId) const REQUIRES(mLock);
Byoungho Jungee6268f2023-10-30 17:27:26 +090098 std::pair<int32_t, PointerControllerInterface&> getDisplayIdAndMouseControllerLocked(
99 int32_t associatedDisplayId) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900100 InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101
102 NotifyMotionArgs processMotion(const NotifyMotionArgs& args);
103 NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungee6268f2023-10-30 17:27:26 +0900104 NotifyMotionArgs processTouchpadEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900105 void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900106 void processStylusHoverEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900107 void processDeviceReset(const NotifyDeviceResetArgs& args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900108
Prabir Pradhan19767602023-11-03 16:53:31 +0000109 using ControllerConstructor =
110 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
Prabir Pradhan16788792023-11-08 21:07:21 +0000111 ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000112 ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900113 ControllerConstructor getStylusControllerConstructor(int32_t displayId) REQUIRES(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000114
Byoungho Jungda10dd32023-10-06 17:03:45 +0900115 std::mutex mLock;
116
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000117 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900118 PointerChoreographerPolicyInterface& mPolicy;
119
120 std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay
121 GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900122 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice
123 GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900124 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice
125 GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900126
127 int32_t mDefaultMouseDisplayId GUARDED_BY(mLock);
128 int32_t mNotifiedPointerDisplayId GUARDED_BY(mLock);
129 std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock);
130 std::vector<DisplayViewport> mViewports GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900131 bool mShowTouchesEnabled GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900132 bool mStylusPointerIconEnabled GUARDED_BY(mLock);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000133};
134
135} // namespace android