1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "InputTarget.h"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <input/PrintTools.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <inttypes.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <string>
24*38e8c45fSAndroid Build Coastguard Worker
25*38e8c45fSAndroid Build Coastguard Worker using android::base::Error;
26*38e8c45fSAndroid Build Coastguard Worker using android::base::Result;
27*38e8c45fSAndroid Build Coastguard Worker using android::base::StringPrintf;
28*38e8c45fSAndroid Build Coastguard Worker
29*38e8c45fSAndroid Build Coastguard Worker namespace android::inputdispatcher {
30*38e8c45fSAndroid Build Coastguard Worker
31*38e8c45fSAndroid Build Coastguard Worker namespace {
32*38e8c45fSAndroid Build Coastguard Worker
33*38e8c45fSAndroid Build Coastguard Worker const static ui::Transform kIdentityTransform{};
34*38e8c45fSAndroid Build Coastguard Worker
35*38e8c45fSAndroid Build Coastguard Worker }
36*38e8c45fSAndroid Build Coastguard Worker
InputTarget(const std::shared_ptr<Connection> & connection,ftl::Flags<Flags> flags)37*38e8c45fSAndroid Build Coastguard Worker InputTarget::InputTarget(const std::shared_ptr<Connection>& connection, ftl::Flags<Flags> flags)
38*38e8c45fSAndroid Build Coastguard Worker : connection(connection), flags(flags) {}
39*38e8c45fSAndroid Build Coastguard Worker
addPointers(std::bitset<MAX_POINTER_ID+1> newPointerIds,const ui::Transform & transform)40*38e8c45fSAndroid Build Coastguard Worker Result<void> InputTarget::addPointers(std::bitset<MAX_POINTER_ID + 1> newPointerIds,
41*38e8c45fSAndroid Build Coastguard Worker const ui::Transform& transform) {
42*38e8c45fSAndroid Build Coastguard Worker // The pointerIds can be empty, but still a valid InputTarget. This can happen when there is no
43*38e8c45fSAndroid Build Coastguard Worker // valid pointer property from the input event.
44*38e8c45fSAndroid Build Coastguard Worker if (newPointerIds.none()) {
45*38e8c45fSAndroid Build Coastguard Worker setDefaultPointerTransform(transform);
46*38e8c45fSAndroid Build Coastguard Worker return {};
47*38e8c45fSAndroid Build Coastguard Worker }
48*38e8c45fSAndroid Build Coastguard Worker
49*38e8c45fSAndroid Build Coastguard Worker // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
50*38e8c45fSAndroid Build Coastguard Worker if ((getPointerIds() & newPointerIds).any()) {
51*38e8c45fSAndroid Build Coastguard Worker return Error() << __func__ << " - overlap with incoming pointers "
52*38e8c45fSAndroid Build Coastguard Worker << bitsetToString(newPointerIds) << " in " << *this;
53*38e8c45fSAndroid Build Coastguard Worker }
54*38e8c45fSAndroid Build Coastguard Worker
55*38e8c45fSAndroid Build Coastguard Worker for (auto& [existingTransform, existingPointers] : mPointerTransforms) {
56*38e8c45fSAndroid Build Coastguard Worker if (transform == existingTransform) {
57*38e8c45fSAndroid Build Coastguard Worker existingPointers |= newPointerIds;
58*38e8c45fSAndroid Build Coastguard Worker return {};
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker }
61*38e8c45fSAndroid Build Coastguard Worker mPointerTransforms.emplace_back(transform, newPointerIds);
62*38e8c45fSAndroid Build Coastguard Worker return {};
63*38e8c45fSAndroid Build Coastguard Worker }
64*38e8c45fSAndroid Build Coastguard Worker
setDefaultPointerTransform(const ui::Transform & transform)65*38e8c45fSAndroid Build Coastguard Worker void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
66*38e8c45fSAndroid Build Coastguard Worker mPointerTransforms = {{transform, {}}};
67*38e8c45fSAndroid Build Coastguard Worker }
68*38e8c45fSAndroid Build Coastguard Worker
useDefaultPointerTransform() const69*38e8c45fSAndroid Build Coastguard Worker bool InputTarget::useDefaultPointerTransform() const {
70*38e8c45fSAndroid Build Coastguard Worker return mPointerTransforms.size() <= 1;
71*38e8c45fSAndroid Build Coastguard Worker }
72*38e8c45fSAndroid Build Coastguard Worker
getDefaultPointerTransform() const73*38e8c45fSAndroid Build Coastguard Worker const ui::Transform& InputTarget::getDefaultPointerTransform() const {
74*38e8c45fSAndroid Build Coastguard Worker if (!useDefaultPointerTransform()) {
75*38e8c45fSAndroid Build Coastguard Worker LOG(FATAL) << __func__ << ": Not using default pointer transform";
76*38e8c45fSAndroid Build Coastguard Worker }
77*38e8c45fSAndroid Build Coastguard Worker return mPointerTransforms.size() == 1 ? mPointerTransforms[0].first : kIdentityTransform;
78*38e8c45fSAndroid Build Coastguard Worker }
79*38e8c45fSAndroid Build Coastguard Worker
getTransformForPointer(int32_t pointerId) const80*38e8c45fSAndroid Build Coastguard Worker const ui::Transform& InputTarget::getTransformForPointer(int32_t pointerId) const {
81*38e8c45fSAndroid Build Coastguard Worker for (const auto& [transform, ids] : mPointerTransforms) {
82*38e8c45fSAndroid Build Coastguard Worker if (ids.test(pointerId)) {
83*38e8c45fSAndroid Build Coastguard Worker return transform;
84*38e8c45fSAndroid Build Coastguard Worker }
85*38e8c45fSAndroid Build Coastguard Worker }
86*38e8c45fSAndroid Build Coastguard Worker
87*38e8c45fSAndroid Build Coastguard Worker LOG(FATAL) << __func__
88*38e8c45fSAndroid Build Coastguard Worker << ": Cannot get transform: The following Pointer ID does not exist in target: "
89*38e8c45fSAndroid Build Coastguard Worker << pointerId;
90*38e8c45fSAndroid Build Coastguard Worker return kIdentityTransform;
91*38e8c45fSAndroid Build Coastguard Worker }
92*38e8c45fSAndroid Build Coastguard Worker
getPointerInfoString() const93*38e8c45fSAndroid Build Coastguard Worker std::string InputTarget::getPointerInfoString() const {
94*38e8c45fSAndroid Build Coastguard Worker std::string out = "\n";
95*38e8c45fSAndroid Build Coastguard Worker if (useDefaultPointerTransform()) {
96*38e8c45fSAndroid Build Coastguard Worker const ui::Transform& transform = getDefaultPointerTransform();
97*38e8c45fSAndroid Build Coastguard Worker transform.dump(out, "default", " ");
98*38e8c45fSAndroid Build Coastguard Worker return out;
99*38e8c45fSAndroid Build Coastguard Worker }
100*38e8c45fSAndroid Build Coastguard Worker
101*38e8c45fSAndroid Build Coastguard Worker for (const auto& [transform, ids] : mPointerTransforms) {
102*38e8c45fSAndroid Build Coastguard Worker const std::string name = "pointerIds " + bitsetToString(ids) + ":";
103*38e8c45fSAndroid Build Coastguard Worker transform.dump(out, name.c_str(), " ");
104*38e8c45fSAndroid Build Coastguard Worker }
105*38e8c45fSAndroid Build Coastguard Worker return out;
106*38e8c45fSAndroid Build Coastguard Worker }
107*38e8c45fSAndroid Build Coastguard Worker
getPointerIds() const108*38e8c45fSAndroid Build Coastguard Worker std::bitset<MAX_POINTER_ID + 1> InputTarget::getPointerIds() const {
109*38e8c45fSAndroid Build Coastguard Worker PointerIds allIds;
110*38e8c45fSAndroid Build Coastguard Worker for (const auto& [_, ids] : mPointerTransforms) {
111*38e8c45fSAndroid Build Coastguard Worker allIds |= ids;
112*38e8c45fSAndroid Build Coastguard Worker }
113*38e8c45fSAndroid Build Coastguard Worker return allIds;
114*38e8c45fSAndroid Build Coastguard Worker }
115*38e8c45fSAndroid Build Coastguard Worker
operator <<(std::ostream & out,const InputTarget & target)116*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const InputTarget& target) {
117*38e8c45fSAndroid Build Coastguard Worker out << "{connection=";
118*38e8c45fSAndroid Build Coastguard Worker if (target.connection != nullptr) {
119*38e8c45fSAndroid Build Coastguard Worker out << target.connection->getInputChannelName();
120*38e8c45fSAndroid Build Coastguard Worker } else {
121*38e8c45fSAndroid Build Coastguard Worker out << "<null>";
122*38e8c45fSAndroid Build Coastguard Worker }
123*38e8c45fSAndroid Build Coastguard Worker out << ", windowHandle=";
124*38e8c45fSAndroid Build Coastguard Worker if (target.windowHandle != nullptr) {
125*38e8c45fSAndroid Build Coastguard Worker out << target.windowHandle->getName();
126*38e8c45fSAndroid Build Coastguard Worker } else {
127*38e8c45fSAndroid Build Coastguard Worker out << "<null>";
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker out << ", dispatchMode=" << ftl::enum_string(target.dispatchMode).c_str();
130*38e8c45fSAndroid Build Coastguard Worker out << ", targetFlags=" << target.flags.string();
131*38e8c45fSAndroid Build Coastguard Worker out << ", pointers=" << target.getPointerInfoString();
132*38e8c45fSAndroid Build Coastguard Worker out << "}";
133*38e8c45fSAndroid Build Coastguard Worker return out;
134*38e8c45fSAndroid Build Coastguard Worker }
135*38e8c45fSAndroid Build Coastguard Worker
136*38e8c45fSAndroid Build Coastguard Worker } // namespace android::inputdispatcher
137