Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 18 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 19 | #include <ftl/flags.h> |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 20 | #include <gui/WindowInfo.h> |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 21 | #include <ui/Transform.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 22 | #include <utils/BitSet.h> |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 23 | #include <bitset> |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 24 | #include "Connection.h" |
| 25 | #include "InputTargetFlags.h" |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android::inputdispatcher { |
| 28 | |
| 29 | /* |
| 30 | * An input target specifies how an input event is to be dispatched to a particular window |
| 31 | * including the window's input channel, control flags, a timeout, and an X / Y offset to |
| 32 | * be added to input event coordinates to compensate for the absolute position of the |
| 33 | * window area. |
| 34 | */ |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 35 | class InputTarget { |
| 36 | public: |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 37 | using Flags = InputTargetFlags; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 38 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 39 | enum class DispatchMode { |
| 40 | /* This flag indicates that the event should be sent as is. |
| 41 | * Should always be set unless the event is to be transmuted. */ |
| 42 | AS_IS, |
| 43 | /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside |
| 44 | * of the area of this target and so should instead be delivered as an |
| 45 | * AMOTION_EVENT_ACTION_OUTSIDE to this target. */ |
| 46 | OUTSIDE, |
| 47 | /* This flag indicates that a hover sequence is starting in the given window. |
| 48 | * The event is transmuted into ACTION_HOVER_ENTER. */ |
| 49 | HOVER_ENTER, |
| 50 | /* This flag indicates that a hover event happened outside of a window which handled |
| 51 | * previous hover events, signifying the end of the current hover sequence for that |
| 52 | * window. |
| 53 | * The event is transmuted into ACTION_HOVER_ENTER. */ |
| 54 | HOVER_EXIT, |
| 55 | /* This flag indicates that the event should be canceled. |
| 56 | * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips |
| 57 | * outside of a window. */ |
| 58 | SLIPPERY_EXIT, |
| 59 | /* This flag indicates that the event should be dispatched as an initial down. |
| 60 | * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips |
| 61 | * into a new window. */ |
| 62 | SLIPPERY_ENTER, |
| 63 | |
| 64 | ftl_last = SLIPPERY_ENTER, |
| 65 | }; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 66 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 67 | // The input connection to be targeted. |
| 68 | std::shared_ptr<Connection> connection; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 69 | |
| 70 | // Flags for the input target. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 71 | ftl::Flags<Flags> flags; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 72 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 73 | // The dispatch mode that should be used for this target. |
| 74 | DispatchMode dispatchMode = DispatchMode::AS_IS; |
| 75 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 76 | // Scaling factor to apply to MotionEvent as it is delivered. |
| 77 | // (ignored for KeyEvents) |
Siarhei Vishniakou | 240f831 | 2019-11-27 17:09:07 -0800 | [diff] [blame] | 78 | float globalScaleFactor = 1.0f; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 79 | |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 80 | // Current display transform. Used for compatibility for raw coordinates. |
| 81 | ui::Transform displayTransform; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 82 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 83 | // Event time for the first motion event (ACTION_DOWN) dispatched to this input target if |
| 84 | // FLAG_SPLIT is set. |
| 85 | std::optional<nsecs_t> firstDownTimeInTarget; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 86 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 87 | // The window that this input target is being dispatched to. It is possible for this to be |
| 88 | // null for cases like global monitors. |
| 89 | sp<gui::WindowInfoHandle> windowHandle; |
| 90 | |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 91 | InputTarget() = default; |
| 92 | InputTarget(const std::shared_ptr<Connection>&, ftl::Flags<Flags> = {}); |
| 93 | |
Siarhei Vishniakou | 5996759 | 2024-04-01 16:17:46 -0700 | [diff] [blame] | 94 | android::base::Result<void> addPointers(std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 95 | const ui::Transform& transform); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 96 | void setDefaultPointerTransform(const ui::Transform& transform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Returns whether the default pointer information should be used. This will be true when the |
| 100 | * InputTarget doesn't have any bits set in the pointerIds bitset. This can happen for monitors |
| 101 | * and non splittable windows since we want all pointers for the EventEntry to go to this |
| 102 | * target. |
| 103 | */ |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 104 | bool useDefaultPointerTransform() const; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 107 | * Returns the default Transform object. This should be used when useDefaultPointerTransform is |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 108 | * true. |
| 109 | */ |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 110 | const ui::Transform& getDefaultPointerTransform() const; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 111 | |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 112 | const ui::Transform& getTransformForPointer(int32_t pointerId) const; |
| 113 | |
| 114 | std::bitset<MAX_POINTER_ID + 1> getPointerIds() const; |
| 115 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 116 | std::string getPointerInfoString() const; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | template <typename K, typename V> |
| 120 | using ArrayMap = std::vector<std::pair<K, V>>; |
| 121 | using PointerIds = std::bitset<MAX_POINTER_ID + 1>; |
| 122 | // The mapping of pointer IDs to the transform that should be used for that collection of IDs. |
| 123 | // Each of the pointer IDs are mutually disjoint, and their union makes up pointer IDs to |
| 124 | // include in the motion events dispatched to this target. We use an ArrayMap to store this to |
| 125 | // avoid having to define hash or comparison functions for ui::Transform, which would be needed |
| 126 | // to use std::unordered_map or std::map respectively. |
| 127 | ArrayMap<ui::Transform, PointerIds> mPointerTransforms; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 130 | std::ostream& operator<<(std::ostream& out, const InputTarget& target); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 131 | |
| 132 | } // namespace android::inputdispatcher |