blob: 26d2fef5c3e315ca841d70ea24fc8ddda25d84e8 [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 Jung6f5b16b2023-10-27 18:22:07 +090098 InputDeviceInfo* findInputDeviceLocked(DeviceId deviceId) REQUIRES(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +090099
100 NotifyMotionArgs processMotion(const NotifyMotionArgs& args);
101 NotifyMotionArgs processMouseEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900102 void processTouchscreenAndStylusEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900103 void processStylusHoverEventLocked(const NotifyMotionArgs& args) REQUIRES(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900104 void processDeviceReset(const NotifyDeviceResetArgs& args);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900105
Prabir Pradhan19767602023-11-03 16:53:31 +0000106 using ControllerConstructor =
107 ConstructorDelegate<std::function<std::shared_ptr<PointerControllerInterface>()>>;
Prabir Pradhan16788792023-11-08 21:07:21 +0000108 ControllerConstructor mTouchControllerConstructor GUARDED_BY(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000109 ControllerConstructor getMouseControllerConstructor(int32_t displayId) REQUIRES(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900110 ControllerConstructor getStylusControllerConstructor(int32_t displayId) REQUIRES(mLock);
Prabir Pradhan19767602023-11-03 16:53:31 +0000111
Byoungho Jungda10dd32023-10-06 17:03:45 +0900112 std::mutex mLock;
113
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000114 InputListenerInterface& mNextListener;
Byoungho Jungda10dd32023-10-06 17:03:45 +0900115 PointerChoreographerPolicyInterface& mPolicy;
116
117 std::map<int32_t, std::shared_ptr<PointerControllerInterface>> mMousePointersByDisplay
118 GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900119 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mTouchPointersByDevice
120 GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900121 std::map<DeviceId, std::shared_ptr<PointerControllerInterface>> mStylusPointersByDevice
122 GUARDED_BY(mLock);
Byoungho Jungda10dd32023-10-06 17:03:45 +0900123
124 int32_t mDefaultMouseDisplayId GUARDED_BY(mLock);
125 int32_t mNotifiedPointerDisplayId GUARDED_BY(mLock);
126 std::vector<InputDeviceInfo> mInputDeviceInfos GUARDED_BY(mLock);
127 std::vector<DisplayViewport> mViewports GUARDED_BY(mLock);
Byoungho Jung6f5b16b2023-10-27 18:22:07 +0900128 bool mShowTouchesEnabled GUARDED_BY(mLock);
Byoungho Jungd6fe27b2023-10-27 20:49:38 +0900129 bool mStylusPointerIconEnabled GUARDED_BY(mLock);
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000130};
131
132} // namespace android