blob: 60a75ee107ff24e1b3a14d49801b666eed81f269 [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -07001/*
2 * Copyright (C) 2019 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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Garfield Tane84e6f92019-08-29 17:28:41 -070018
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080019#include <ftl/flags.h>
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000020#include <gui/WindowInfo.h>
chaviw3277faf2021-05-19 16:45:23 -050021#include <gui/constants.h>
chaviw1ff3d1e2020-07-01 15:53:47 -070022#include <ui/Transform.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070023#include <utils/BitSet.h>
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080024#include <bitset>
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -080025#include "Connection.h"
26#include "InputTargetFlags.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070027
28namespace android::inputdispatcher {
29
30/*
31 * An input target specifies how an input event is to be dispatched to a particular window
32 * including the window's input channel, control flags, a timeout, and an X / Y offset to
33 * be added to input event coordinates to compensate for the absolute position of the
34 * window area.
35 */
Prabir Pradhanc32a4112024-01-23 23:20:37 +000036class InputTarget {
37public:
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -080038 using Flags = InputTargetFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -070039
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +000040 enum class DispatchMode {
41 /* This flag indicates that the event should be sent as is.
42 * Should always be set unless the event is to be transmuted. */
43 AS_IS,
44 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
45 * of the area of this target and so should instead be delivered as an
46 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
47 OUTSIDE,
48 /* This flag indicates that a hover sequence is starting in the given window.
49 * The event is transmuted into ACTION_HOVER_ENTER. */
50 HOVER_ENTER,
51 /* This flag indicates that a hover event happened outside of a window which handled
52 * previous hover events, signifying the end of the current hover sequence for that
53 * window.
54 * The event is transmuted into ACTION_HOVER_ENTER. */
55 HOVER_EXIT,
56 /* This flag indicates that the event should be canceled.
57 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
58 * outside of a window. */
59 SLIPPERY_EXIT,
60 /* This flag indicates that the event should be dispatched as an initial down.
61 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
62 * into a new window. */
63 SLIPPERY_ENTER,
64
65 ftl_last = SLIPPERY_ENTER,
66 };
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080067
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -080068 // The input connection to be targeted.
69 std::shared_ptr<Connection> connection;
Garfield Tane84e6f92019-08-29 17:28:41 -070070
71 // Flags for the input target.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080072 ftl::Flags<Flags> flags;
Garfield Tane84e6f92019-08-29 17:28:41 -070073
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +000074 // The dispatch mode that should be used for this target.
75 DispatchMode dispatchMode = DispatchMode::AS_IS;
76
Garfield Tane84e6f92019-08-29 17:28:41 -070077 // Scaling factor to apply to MotionEvent as it is delivered.
78 // (ignored for KeyEvents)
Siarhei Vishniakou240f8312019-11-27 17:09:07 -080079 float globalScaleFactor = 1.0f;
Garfield Tane84e6f92019-08-29 17:28:41 -070080
Prabir Pradhanb9b18502021-08-26 12:30:32 -070081 // Current display transform. Used for compatibility for raw coordinates.
82 ui::Transform displayTransform;
Evan Rosky84f07f02021-04-16 10:42:42 -070083
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000084 // Event time for the first motion event (ACTION_DOWN) dispatched to this input target if
85 // FLAG_SPLIT is set.
86 std::optional<nsecs_t> firstDownTimeInTarget;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000087
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000088 // The window that this input target is being dispatched to. It is possible for this to be
89 // null for cases like global monitors.
90 sp<gui::WindowInfoHandle> windowHandle;
91
Prabir Pradhanc32a4112024-01-23 23:20:37 +000092 InputTarget() = default;
93 InputTarget(const std::shared_ptr<Connection>&, ftl::Flags<Flags> = {});
94
Siarhei Vishniakou59967592024-04-01 16:17:46 -070095 android::base::Result<void> addPointers(std::bitset<MAX_POINTER_ID + 1> pointerIds,
96 const ui::Transform& transform);
chaviw1ff3d1e2020-07-01 15:53:47 -070097 void setDefaultPointerTransform(const ui::Transform& transform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000098
99 /**
100 * Returns whether the default pointer information should be used. This will be true when the
101 * InputTarget doesn't have any bits set in the pointerIds bitset. This can happen for monitors
102 * and non splittable windows since we want all pointers for the EventEntry to go to this
103 * target.
104 */
chaviw1ff3d1e2020-07-01 15:53:47 -0700105 bool useDefaultPointerTransform() const;
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000106
107 /**
chaviw1ff3d1e2020-07-01 15:53:47 -0700108 * Returns the default Transform object. This should be used when useDefaultPointerTransform is
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000109 * true.
110 */
chaviw1ff3d1e2020-07-01 15:53:47 -0700111 const ui::Transform& getDefaultPointerTransform() const;
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000112
Prabir Pradhanc32a4112024-01-23 23:20:37 +0000113 const ui::Transform& getTransformForPointer(int32_t pointerId) const;
114
115 std::bitset<MAX_POINTER_ID + 1> getPointerIds() const;
116
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000117 std::string getPointerInfoString() const;
Prabir Pradhanc32a4112024-01-23 23:20:37 +0000118
119private:
120 template <typename K, typename V>
121 using ArrayMap = std::vector<std::pair<K, V>>;
122 using PointerIds = std::bitset<MAX_POINTER_ID + 1>;
123 // The mapping of pointer IDs to the transform that should be used for that collection of IDs.
124 // Each of the pointer IDs are mutually disjoint, and their union makes up pointer IDs to
125 // include in the motion events dispatched to this target. We use an ArrayMap to store this to
126 // avoid having to define hash or comparison functions for ui::Transform, which would be needed
127 // to use std::unordered_map or std::map respectively.
128 ArrayMap<ui::Transform, PointerIds> mPointerTransforms;
Garfield Tane84e6f92019-08-29 17:28:41 -0700129};
130
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700131std::ostream& operator<<(std::ostream& out, const InputTarget& target);
Garfield Tane84e6f92019-08-29 17:28:41 -0700132
133} // namespace android::inputdispatcher