blob: 343630c0da7068941834f5368a5bb388cdd33e5a [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
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080029void InputTarget::addPointers(std::bitset<MAX_POINTER_ID + 1> newPointerIds,
30 const ui::Transform& transform) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000031 // The pointerIds can be empty, but still a valid InputTarget. This can happen when there is no
32 // valid pointer property from the input event.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080033 if (newPointerIds.none()) {
chaviw1ff3d1e2020-07-01 15:53:47 -070034 setDefaultPointerTransform(transform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000035 return;
36 }
37
38 // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070039 if ((pointerIds & newPointerIds).any()) {
40 LOG(FATAL) << __func__ << " - overlap with incoming pointers "
41 << bitsetToString(newPointerIds) << " in " << *this;
42 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +000043
44 pointerIds |= newPointerIds;
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080045 for (size_t i = 0; i < newPointerIds.size(); i++) {
46 if (!newPointerIds.test(i)) {
47 continue;
48 }
49 pointerTransforms[i] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000050 }
51}
52
chaviw1ff3d1e2020-07-01 15:53:47 -070053void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080054 pointerIds.reset();
chaviw1ff3d1e2020-07-01 15:53:47 -070055 pointerTransforms[0] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000056}
57
chaviw1ff3d1e2020-07-01 15:53:47 -070058bool InputTarget::useDefaultPointerTransform() const {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080059 return pointerIds.none();
Chavi Weingarten65f98b82020-01-16 18:56:50 +000060}
61
chaviw1ff3d1e2020-07-01 15:53:47 -070062const ui::Transform& InputTarget::getDefaultPointerTransform() const {
63 return pointerTransforms[0];
Chavi Weingarten65f98b82020-01-16 18:56:50 +000064}
65
66std::string InputTarget::getPointerInfoString() const {
chaviw85b44202020-07-24 11:46:21 -070067 std::string out = "\n";
chaviw1ff3d1e2020-07-01 15:53:47 -070068 if (useDefaultPointerTransform()) {
69 const ui::Transform& transform = getDefaultPointerTransform();
chaviw85b44202020-07-24 11:46:21 -070070 transform.dump(out, "default", " ");
chaviw1ff3d1e2020-07-01 15:53:47 -070071 return out;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000072 }
73
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080074 for (uint32_t i = 0; i < pointerIds.size(); i++) {
75 if (!pointerIds.test(i)) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +000076 continue;
77 }
chaviw1ff3d1e2020-07-01 15:53:47 -070078
chaviw1ff3d1e2020-07-01 15:53:47 -070079 const std::string name = "pointerId " + std::to_string(i) + ":";
chaviw85b44202020-07-24 11:46:21 -070080 pointerTransforms[i].dump(out, name.c_str(), " ");
Chavi Weingarten65f98b82020-01-16 18:56:50 +000081 }
82 return out;
83}
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070084
85std::ostream& operator<<(std::ostream& out, const InputTarget& target) {
86 out << "{inputChannel=";
87 if (target.inputChannel != nullptr) {
88 out << target.inputChannel->getName();
89 } else {
90 out << "<null>";
91 }
92 out << ", windowHandle=";
93 if (target.windowHandle != nullptr) {
94 out << target.windowHandle->getName();
95 } else {
96 out << "<null>";
97 }
98 out << ", targetFlags=" << target.flags.string();
99 out << ", pointers=" << target.getPointerInfoString();
100 out << "}";
101 return out;
102}
103
Garfield Tane84e6f92019-08-29 17:28:41 -0700104} // namespace android::inputdispatcher