blob: 11f3413df8ec425354dbff68fce0ab1b310c705b [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
19#include <android-base/stringprintf.h>
20#include <inttypes.h>
21#include <string>
22
23using android::base::StringPrintf;
24
25namespace android::inputdispatcher {
26
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080027void InputTarget::addPointers(std::bitset<MAX_POINTER_ID + 1> newPointerIds,
28 const ui::Transform& transform) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000029 // The pointerIds can be empty, but still a valid InputTarget. This can happen when there is no
30 // valid pointer property from the input event.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080031 if (newPointerIds.none()) {
chaviw1ff3d1e2020-07-01 15:53:47 -070032 setDefaultPointerTransform(transform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000033 return;
34 }
35
36 // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080037 LOG_ALWAYS_FATAL_IF((pointerIds & newPointerIds).any());
Chavi Weingarten65f98b82020-01-16 18:56:50 +000038
39 pointerIds |= newPointerIds;
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080040 for (size_t i = 0; i < newPointerIds.size(); i++) {
41 if (!newPointerIds.test(i)) {
42 continue;
43 }
44 pointerTransforms[i] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000045 }
46}
47
chaviw1ff3d1e2020-07-01 15:53:47 -070048void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080049 pointerIds.reset();
chaviw1ff3d1e2020-07-01 15:53:47 -070050 pointerTransforms[0] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000051}
52
chaviw1ff3d1e2020-07-01 15:53:47 -070053bool InputTarget::useDefaultPointerTransform() const {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080054 return pointerIds.none();
Chavi Weingarten65f98b82020-01-16 18:56:50 +000055}
56
chaviw1ff3d1e2020-07-01 15:53:47 -070057const ui::Transform& InputTarget::getDefaultPointerTransform() const {
58 return pointerTransforms[0];
Chavi Weingarten65f98b82020-01-16 18:56:50 +000059}
60
61std::string InputTarget::getPointerInfoString() const {
chaviw85b44202020-07-24 11:46:21 -070062 std::string out = "\n";
chaviw1ff3d1e2020-07-01 15:53:47 -070063 if (useDefaultPointerTransform()) {
64 const ui::Transform& transform = getDefaultPointerTransform();
chaviw85b44202020-07-24 11:46:21 -070065 transform.dump(out, "default", " ");
chaviw1ff3d1e2020-07-01 15:53:47 -070066 return out;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000067 }
68
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080069 for (uint32_t i = 0; i < pointerIds.size(); i++) {
70 if (!pointerIds.test(i)) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +000071 continue;
72 }
chaviw1ff3d1e2020-07-01 15:53:47 -070073
chaviw1ff3d1e2020-07-01 15:53:47 -070074 const std::string name = "pointerId " + std::to_string(i) + ":";
chaviw85b44202020-07-24 11:46:21 -070075 pointerTransforms[i].dump(out, name.c_str(), " ");
Chavi Weingarten65f98b82020-01-16 18:56:50 +000076 }
77 return out;
78}
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070079
80std::ostream& operator<<(std::ostream& out, const InputTarget& target) {
81 out << "{inputChannel=";
82 if (target.inputChannel != nullptr) {
83 out << target.inputChannel->getName();
84 } else {
85 out << "<null>";
86 }
87 out << ", windowHandle=";
88 if (target.windowHandle != nullptr) {
89 out << target.windowHandle->getName();
90 } else {
91 out << "<null>";
92 }
93 out << ", targetFlags=" << target.flags.string();
94 out << ", pointers=" << target.getPointerInfoString();
95 out << "}";
96 return out;
97}
98
Garfield Tane84e6f92019-08-29 17:28:41 -070099} // namespace android::inputdispatcher