1 /*
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
23 using android::base::StringPrintf;
24
25 namespace android::inputdispatcher {
26
dispatchModeToString(int32_t dispatchMode)27 std::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
addPointers(BitSet32 newPointerIds,const ui::Transform & transform)45 void InputTarget::addPointers(BitSet32 newPointerIds, const ui::Transform& transform) {
46 // The pointerIds can be empty, but still a valid InputTarget. This can happen for Monitors
47 // and non splittable windows since we will just use all the pointers from the input event.
48 if (newPointerIds.isEmpty()) {
49 setDefaultPointerTransform(transform);
50 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();
59 pointerTransforms[pointerId] = transform;
60 }
61 }
62
setDefaultPointerTransform(const ui::Transform & transform)63 void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
64 pointerIds.clear();
65 pointerTransforms[0] = transform;
66 }
67
useDefaultPointerTransform() const68 bool InputTarget::useDefaultPointerTransform() const {
69 return pointerIds.isEmpty();
70 }
71
getDefaultPointerTransform() const72 const ui::Transform& InputTarget::getDefaultPointerTransform() const {
73 return pointerTransforms[0];
74 }
75
getPointerInfoString() const76 std::string InputTarget::getPointerInfoString() const {
77 std::string out = "\n";
78 if (useDefaultPointerTransform()) {
79 const ui::Transform& transform = getDefaultPointerTransform();
80 transform.dump(out, "default", " ");
81 return out;
82 }
83
84 for (uint32_t i = pointerIds.firstMarkedBit(); i <= pointerIds.lastMarkedBit(); i++) {
85 if (!pointerIds.hasBit(i)) {
86 continue;
87 }
88
89 const std::string name = "pointerId " + std::to_string(i) + ":";
90 pointerTransforms[i].dump(out, name.c_str(), " ");
91 }
92 return out;
93 }
94 } // namespace android::inputdispatcher
95