• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
addPointers(std::bitset<MAX_POINTER_ID+1> newPointerIds,const ui::Transform & transform)27 void InputTarget::addPointers(std::bitset<MAX_POINTER_ID + 1> newPointerIds,
28                               const ui::Transform& transform) {
29     // 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.
31     if (newPointerIds.none()) {
32         setDefaultPointerTransform(transform);
33         return;
34     }
35 
36     // Ensure that the new set of pointers doesn't overlap with the current set of pointers.
37     LOG_ALWAYS_FATAL_IF((pointerIds & newPointerIds).any());
38 
39     pointerIds |= newPointerIds;
40     for (size_t i = 0; i < newPointerIds.size(); i++) {
41         if (!newPointerIds.test(i)) {
42             continue;
43         }
44         pointerTransforms[i] = transform;
45     }
46 }
47 
setDefaultPointerTransform(const ui::Transform & transform)48 void InputTarget::setDefaultPointerTransform(const ui::Transform& transform) {
49     pointerIds.reset();
50     pointerTransforms[0] = transform;
51 }
52 
useDefaultPointerTransform() const53 bool InputTarget::useDefaultPointerTransform() const {
54     return pointerIds.none();
55 }
56 
getDefaultPointerTransform() const57 const ui::Transform& InputTarget::getDefaultPointerTransform() const {
58     return pointerTransforms[0];
59 }
60 
getPointerInfoString() const61 std::string InputTarget::getPointerInfoString() const {
62     std::string out = "\n";
63     if (useDefaultPointerTransform()) {
64         const ui::Transform& transform = getDefaultPointerTransform();
65         transform.dump(out, "default", "        ");
66         return out;
67     }
68 
69     for (uint32_t i = 0; i < pointerIds.size(); i++) {
70         if (!pointerIds.test(i)) {
71             continue;
72         }
73 
74         const std::string name = "pointerId " + std::to_string(i) + ":";
75         pointerTransforms[i].dump(out, name.c_str(), "        ");
76     }
77     return out;
78 }
79 } // namespace android::inputdispatcher
80