blob: 2f39480555f5ee99dc685c4f6f097b1afb97edc9 [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
chaviw1ff3d1e2020-07-01 15:53:47 -070027void InputTarget::addPointers(BitSet32 newPointerIds, const ui::Transform& transform) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000028 // The pointerIds can be empty, but still a valid InputTarget. This can happen when there is no
29 // valid pointer property from the input event.
Chavi Weingarten65f98b82020-01-16 18:56:50 +000030 if (newPointerIds.isEmpty()) {
chaviw1ff3d1e2020-07-01 15:53:47 -070031 setDefaultPointerTransform(transform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000032 return;
33 }
34
35 // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
36 ALOG_ASSERT((pointerIds & newPointerIds) == 0);
37
38 pointerIds |= newPointerIds;
39 while (!newPointerIds.isEmpty()) {
40 int32_t pointerId = newPointerIds.clearFirstMarkedBit();
chaviw1ff3d1e2020-07-01 15:53:47 -070041 pointerTransforms[pointerId] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000042 }
43}
44
chaviw1ff3d1e2020-07-01 15:53:47 -070045void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +000046 pointerIds.clear();
chaviw1ff3d1e2020-07-01 15:53:47 -070047 pointerTransforms[0] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000048}
49
chaviw1ff3d1e2020-07-01 15:53:47 -070050bool InputTarget::useDefaultPointerTransform() const {
Chavi Weingarten65f98b82020-01-16 18:56:50 +000051 return pointerIds.isEmpty();
52}
53
chaviw1ff3d1e2020-07-01 15:53:47 -070054const ui::Transform& InputTarget::getDefaultPointerTransform() const {
55 return pointerTransforms[0];
Chavi Weingarten65f98b82020-01-16 18:56:50 +000056}
57
58std::string InputTarget::getPointerInfoString() const {
chaviw85b44202020-07-24 11:46:21 -070059 std::string out = "\n";
chaviw1ff3d1e2020-07-01 15:53:47 -070060 if (useDefaultPointerTransform()) {
61 const ui::Transform& transform = getDefaultPointerTransform();
chaviw85b44202020-07-24 11:46:21 -070062 transform.dump(out, "default", " ");
chaviw1ff3d1e2020-07-01 15:53:47 -070063 return out;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000064 }
65
Chavi Weingarten65f98b82020-01-16 18:56:50 +000066 for (uint32_t i = pointerIds.firstMarkedBit(); i <= pointerIds.lastMarkedBit(); i++) {
67 if (!pointerIds.hasBit(i)) {
68 continue;
69 }
chaviw1ff3d1e2020-07-01 15:53:47 -070070
chaviw1ff3d1e2020-07-01 15:53:47 -070071 const std::string name = "pointerId " + std::to_string(i) + ":";
chaviw85b44202020-07-24 11:46:21 -070072 pointerTransforms[i].dump(out, name.c_str(), " ");
Chavi Weingarten65f98b82020-01-16 18:56:50 +000073 }
74 return out;
75}
Garfield Tane84e6f92019-08-29 17:28:41 -070076} // namespace android::inputdispatcher