blob: 2df97d9a12df325caa63dcf21117784047bcf602 [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
27std::string dispatchModeToString(int32_t dispatchMode) {
28 switch (dispatchMode) {
29 case InputTarget::FLAG_DISPATCH_AS_IS:
30 return "DISPATCH_AS_IS";
31 case InputTarget::FLAG_DISPATCH_AS_OUTSIDE:
32 return "DISPATCH_AS_OUTSIDE";
33 case InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER:
34 return "DISPATCH_AS_HOVER_ENTER";
35 case InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT:
36 return "DISPATCH_AS_HOVER_EXIT";
37 case InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT:
38 return "DISPATCH_AS_SLIPPERY_EXIT";
39 case InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER:
40 return "DISPATCH_AS_SLIPPERY_ENTER";
41 }
42 return StringPrintf("%" PRId32, dispatchMode);
43}
44
chaviw1ff3d1e2020-07-01 15:53:47 -070045void InputTarget::addPointers(BitSet32 newPointerIds, const ui::Transform& transform) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +000046 // The pointerIds can be empty, but still a valid InputTarget. This can happen when there is no
47 // valid pointer property from the input event.
Chavi Weingarten65f98b82020-01-16 18:56:50 +000048 if (newPointerIds.isEmpty()) {
chaviw1ff3d1e2020-07-01 15:53:47 -070049 setDefaultPointerTransform(transform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +000050 return;
51 }
52
53 // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
54 ALOG_ASSERT((pointerIds & newPointerIds) == 0);
55
56 pointerIds |= newPointerIds;
57 while (!newPointerIds.isEmpty()) {
58 int32_t pointerId = newPointerIds.clearFirstMarkedBit();
chaviw1ff3d1e2020-07-01 15:53:47 -070059 pointerTransforms[pointerId] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000060 }
61}
62
chaviw1ff3d1e2020-07-01 15:53:47 -070063void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +000064 pointerIds.clear();
chaviw1ff3d1e2020-07-01 15:53:47 -070065 pointerTransforms[0] = transform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000066}
67
chaviw1ff3d1e2020-07-01 15:53:47 -070068bool InputTarget::useDefaultPointerTransform() const {
Chavi Weingarten65f98b82020-01-16 18:56:50 +000069 return pointerIds.isEmpty();
70}
71
chaviw1ff3d1e2020-07-01 15:53:47 -070072const ui::Transform& InputTarget::getDefaultPointerTransform() const {
73 return pointerTransforms[0];
Chavi Weingarten65f98b82020-01-16 18:56:50 +000074}
75
76std::string InputTarget::getPointerInfoString() const {
chaviw85b44202020-07-24 11:46:21 -070077 std::string out = "\n";
chaviw1ff3d1e2020-07-01 15:53:47 -070078 if (useDefaultPointerTransform()) {
79 const ui::Transform& transform = getDefaultPointerTransform();
chaviw85b44202020-07-24 11:46:21 -070080 transform.dump(out, "default", " ");
chaviw1ff3d1e2020-07-01 15:53:47 -070081 return out;
Chavi Weingarten65f98b82020-01-16 18:56:50 +000082 }
83
Chavi Weingarten65f98b82020-01-16 18:56:50 +000084 for (uint32_t i = pointerIds.firstMarkedBit(); i <= pointerIds.lastMarkedBit(); i++) {
85 if (!pointerIds.hasBit(i)) {
86 continue;
87 }
chaviw1ff3d1e2020-07-01 15:53:47 -070088
chaviw1ff3d1e2020-07-01 15:53:47 -070089 const std::string name = "pointerId " + std::to_string(i) + ":";
chaviw85b44202020-07-24 11:46:21 -070090 pointerTransforms[i].dump(out, name.c_str(), " ");
Chavi Weingarten65f98b82020-01-16 18:56:50 +000091 }
92 return out;
93}
Garfield Tane84e6f92019-08-29 17:28:41 -070094} // namespace android::inputdispatcher