blob: 35ad8587367eb992949159d660ee2285fc9efea9 [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
17#include "InputTarget.h"
18
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070019#include <android-base/logging.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070020#include <android-base/stringprintf.h>
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070021#include <input/PrintTools.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070022#include <inttypes.h>
23#include <string>
24
25using android::base::StringPrintf;
26
27namespace android::inputdispatcher {
28
Prabir Pradhanc32a4112024-01-23 23:20:37 +000029namespace {
30
31const static ui::Transform kIdentityTransform{};
32
33}
34
35InputTarget::InputTarget(const std::shared_ptr<Connection>& connection, ftl::Flags<Flags> flags)
36 : connection(connection), flags(flags) {}
37
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080038void InputTarget::addPointers(std::bitset<MAX_POINTER_ID + 1> newPointerIds,
39 const ui::Transform& transform) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000040 // The pointerIds can be empty, but still a valid InputTarget. This can happen when there is no
41 // valid pointer property from the input event.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080042 if (newPointerIds.none()) {
chaviw1ff3d1e2020-07-01 15:53:47 -070043 setDefaultPointerTransform(transform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000044 return;
45 }
46
47 // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
Prabir Pradhanc32a4112024-01-23 23:20:37 +000048 if ((getPointerIds() & newPointerIds).any()) {
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070049 LOG(FATAL) << __func__ << " - overlap with incoming pointers "
50 << bitsetToString(newPointerIds) << " in " << *this;
51 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +000052
Prabir Pradhanc32a4112024-01-23 23:20:37 +000053 for (auto& [existingTransform, existingPointers] : mPointerTransforms) {
54 if (transform == existingTransform) {
55 existingPointers |= newPointerIds;
56 return;
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080057 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +000058 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +000059 mPointerTransforms.emplace_back(transform, newPointerIds);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000060}
61
chaviw1ff3d1e2020-07-01 15:53:47 -070062void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
Prabir Pradhanc32a4112024-01-23 23:20:37 +000063 mPointerTransforms = {{transform, {}}};
Chavi Weingarten65f98b82020-01-16 18:56:50 +000064}
65
chaviw1ff3d1e2020-07-01 15:53:47 -070066bool InputTarget::useDefaultPointerTransform() const {
Prabir Pradhanc32a4112024-01-23 23:20:37 +000067 return mPointerTransforms.size() <= 1;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000068}
69
chaviw1ff3d1e2020-07-01 15:53:47 -070070const ui::Transform& InputTarget::getDefaultPointerTransform() const {
Prabir Pradhanc32a4112024-01-23 23:20:37 +000071 if (!useDefaultPointerTransform()) {
72 LOG(FATAL) << __func__ << ": Not using default pointer transform";
73 }
74 return mPointerTransforms.size() == 1 ? mPointerTransforms[0].first : kIdentityTransform;
75}
76
77const ui::Transform& InputTarget::getTransformForPointer(int32_t pointerId) const {
78 for (const auto& [transform, ids] : mPointerTransforms) {
79 if (ids.test(pointerId)) {
80 return transform;
81 }
82 }
83
84 LOG(FATAL) << __func__
85 << ": Cannot get transform: The following Pointer ID does not exist in target: "
86 << pointerId;
87 return kIdentityTransform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000088}
89
90std::string InputTarget::getPointerInfoString() const {
chaviw85b44202020-07-24 11:46:21 -070091 std::string out = "\n";
chaviw1ff3d1e2020-07-01 15:53:47 -070092 if (useDefaultPointerTransform()) {
93 const ui::Transform& transform = getDefaultPointerTransform();
chaviw85b44202020-07-24 11:46:21 -070094 transform.dump(out, "default", " ");
chaviw1ff3d1e2020-07-01 15:53:47 -070095 return out;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000096 }
97
Prabir Pradhanc32a4112024-01-23 23:20:37 +000098 for (const auto& [transform, ids] : mPointerTransforms) {
99 const std::string name = "pointerIds " + bitsetToString(ids) + ":";
100 transform.dump(out, name.c_str(), " ");
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000101 }
102 return out;
103}
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700104
Prabir Pradhanc32a4112024-01-23 23:20:37 +0000105std::bitset<MAX_POINTER_ID + 1> InputTarget::getPointerIds() const {
106 PointerIds allIds;
107 for (const auto& [_, ids] : mPointerTransforms) {
108 allIds |= ids;
109 }
110 return allIds;
111}
112
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700113std::ostream& operator<<(std::ostream& out, const InputTarget& target) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800114 out << "{connection=";
115 if (target.connection != nullptr) {
116 out << target.connection->getInputChannelName();
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700117 } else {
118 out << "<null>";
119 }
120 out << ", windowHandle=";
121 if (target.windowHandle != nullptr) {
122 out << target.windowHandle->getName();
123 } else {
124 out << "<null>";
125 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000126 out << ", dispatchMode=" << ftl::enum_string(target.dispatchMode).c_str();
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700127 out << ", targetFlags=" << target.flags.string();
128 out << ", pointers=" << target.getPointerInfoString();
129 out << "}";
130 return out;
131}
132
Garfield Tane84e6f92019-08-29 17:28:41 -0700133} // namespace android::inputdispatcher